JavaScript implementation of Gzip [closed] JavaScript implementation of Gzip [closed] ajax ajax

JavaScript implementation of Gzip [closed]


Edit There appears to be a better LZW solution that handles Unicode strings correctly at http://pieroxy.net/blog/pages/lz-string/index.html (Thanks to pieroxy in the comments).


I don't know of any gzip implementations, but the jsolait library (the site seems to have gone away) has functions for LZW compression/decompression. The code is covered under the LGPL.

// LZW-compress a stringfunction lzw_encode(s) {    var dict = {};    var data = (s + "").split("");    var out = [];    var currChar;    var phrase = data[0];    var code = 256;    for (var i=1; i<data.length; i++) {        currChar=data[i];        if (dict[phrase + currChar] != null) {            phrase += currChar;        }        else {            out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));            dict[phrase + currChar] = code;            code++;            phrase=currChar;        }    }    out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));    for (var i=0; i<out.length; i++) {        out[i] = String.fromCharCode(out[i]);    }    return out.join("");}// Decompress an LZW-encoded stringfunction lzw_decode(s) {    var dict = {};    var data = (s + "").split("");    var currChar = data[0];    var oldPhrase = currChar;    var out = [currChar];    var code = 256;    var phrase;    for (var i=1; i<data.length; i++) {        var currCode = data[i].charCodeAt(0);        if (currCode < 256) {            phrase = data[i];        }        else {           phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);        }        out.push(phrase);        currChar = phrase.charAt(0);        dict[code] = oldPhrase + currChar;        code++;        oldPhrase = phrase;    }    return out.join("");}


I had another problem, I did not want to encode data in gzip but to decode gzipped data. I am running javascript code outside of the browser so I need to decode it using pure javascript.

It took me some time but i found that in the JSXGraph library there is a way to read gzipped data.

Here is where I found the library: http://jsxgraph.uni-bayreuth.de/wp/2009/09/29/jsxcompressor-zlib-compressed-javascript-code/There is even a standalone utility that can do that, JSXCompressor, and the code is LGPL licencied.

Just include the jsxcompressor.js file in your project and then you will be able to read a base 64 encoded gzipped data:

<!doctype html></head><title>Test gzip decompression page</title><script src="jsxcompressor.js"></script></head><body><script>    document.write(JXG.decompress('<?php         echo base64_encode(gzencode("Try not. Do, or do not. There is no try."));     ?>'));</script></html>

I understand it is not what you wanted but I still reply here because I suspect it will help some people.


We just released pako https://github.com/nodeca/pako , port of zlib to javascript. I think that's now the fastest js implementation of deflate / inflate / gzip / ungzip. Also, it has democratic MIT licence. Pako supports all zlib options and it's results are binary equal.

Example:

var inflate = require('pako/lib/inflate').inflate; var text = inflate(zipped, {to: 'string'});