ajax - JavaScript implementation of Gzip -
i'm writing web application needs store json data in small, fixed-size server-side cache via ajax (think: opensocial quotas). not have control on server.
i need reduce size of stored data stay within server-side quota, , hoping able gzip stringified json in browser before sending server.
however, cannot find in way of javascript implementations of gzip. suggestions how can compress data on client side before sending up?
edit there appears better lzw solution handles unicode strings correctly @ http://pieroxy.net/blog/pages/lz-string/index.html (thanks pieroxy in comments).
i don't know of gzip implementations, jsolait library (the site seems have gone away) has functions lzw compression/decompression. code covered under lgpl.
// lzw-compress string function lzw_encode(s) { var dict = {}; var data = (s + "").split(""); var out = []; var currchar; var phrase = data[0]; var code = 256; (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)); (var i=0; i<out.length; i++) { out[i] = string.fromcharcode(out[i]); } return out.join(""); } // decompress lzw-encoded string function lzw_decode(s) { var dict = {}; var data = (s + "").split(""); var currchar = data[0]; var oldphrase = currchar; var out = [currchar]; var code = 256; var phrase; (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(""); }
Comments
Post a Comment