Author Topic: Chilkat LZW Decompression in JavaScript  (Read 235 times)

iifuzz

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Chilkat LZW Decompression in JavaScript
« on: February 14, 2018, 09:29:06 AM »
I have data which I have compressed using chilkat LZW in C#
I am trying to decompress this data in javascript, but cannot seem to find anything that returns valid results.

Any help would help a TON!

Example javascript
    LZW_decode(compressed) {
         // Build the dictionary.
         var i,
             dictionary = [],
             w,
             result,
             k,
             entry = "",
             dictSize = 256;

         for (i = 0; i < 256; i += 1) {
             dictionary = String.fromCharCode(i);
         }

         w = String.fromCharCode(compressed[0]);
         result = w;

         for (i = 1; i < compressed.length; i += 1) {
             k = compressed;
             if (dictionary[k]) {
                 entry = dictionary[k];
             } else {
                 if (k === dictSize) {
                     entry = w + w.charAt(0);
                 } else {
                     return null;
                 }
             }

             result += entry;

             // Add w+entry[0] to the dictionary.
             dictionary[dictSize++] = w + entry.charAt(0);

             w = entry;
         }

         return result;
     }