Is there a library available for compression in Javascript

12k views Asked by At

I am looking for sending data from server in a compressed format to client(with ajax requests), and than decompress that data with a browser? Is there a library for this?

I am not looking for compressing javascript files!

EDIT: I think question was not clear enough, i don't want to compress html files, i want to store some compressed LZMA files or any other compression format on server(like an obj file), and then i need to decompress them after i got it with AJAX. Not simultaneous compression/decompression with gzip. Opening already zipeed files after getting them with Javascript.

5

There are 5 answers

2
Chris Farmiloe On

Your web-server (and the browser) should be capable of handling this transparently using gzip. How this is setup will depend on which server you are using.

Checkout mod_deflate in apache or enabling gzip in nginx.

The browser will automatically decompress the data before it reaches your XHR handler and you can be safe in the knowledge that your data was compressed as much as possible in transit.

0
Archie On

I ported LZMA to GWT a while back. If you're using GWT this might be a viable option.

https://code.google.com/p/gwt-lzma/

0
Orwellophile On

What Erik said, http://code.google.com/p/jslzjb/

Here's some basic code to get you going, and decoding.

 var stringStreamIn = function(s) {
     this.data = s;
     this.length = this.data.length;
     this.offset = -1;
     this.readByte = function(){
         if (++this.offset >= this.length) {
             return null;
         }
         return this.data.charCodeAt(this.offset);
     };
 };

 var stringStreamOut = function() {
     this.data = '';
     this.length = function() { return this.data.length; };
     this.writeByte = function(value) {
         this.data += String.fromCharCode(value);
     };
 };

 var so = new stringStreamOut();
 var si = new stringStreamIn(atob("XQAAgAD//////////wAnG8AHA8+NCpYMj8Bgfez6bsJh4zoZx3fA+gih10oOa6rwYfkaucJIKX8T69I5iOe8WJwg/Ta7x3eHeDomxR6Vf824NLmKSrWHKdnM9n0D2aipzLbHv5//sTGAAA=="));
 LZMA.decompressFile(si, so);
 console.log(so.data);
0
luksch On

I know, this is a very late answer, but I found this an interesting alternative: http://pieroxy.net/blog/pages/lz-string/index.html It also has implementations in other languages!

And my favorite right now is pako. It is really blazing fast and easy to use and compatible to the well known zlib

2
Erik On

This looks promising: http://code.google.com/p/jslzjb/