9

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
gkaykck
  • 2,347
  • 10
  • 35
  • 52
  • For static resources, a properly configured server will already do compression for you, and the browser will automatically decompress. For dynamic ones like you want to query, it is more tricky but possible as well. What server-side language do you use? – Pekka Feb 27 '11 at 22:19
  • Please note that HTTP/1.1 already supports three different compression formats ('gzip', 'deflate', and 'compress'). – Josef Pfleger Feb 27 '11 at 22:20
  • @gkaytck: Does anything http://stackoverflow.com/questions/2349210/how-to-compress-data-on-asp-net-and-uncompress-in-javascript help? – wilbbe01 Feb 27 '11 at 22:25
  • I am not quite sure why you insist on de/compressing on the client site. Javascript is not well designed to operate byte arrays, and the hashing will be awkward. Yet, it's easily doable, just cumbersome. You can use server side compression/decompression and gzip to transfer to the client. It's an easier solution. – bestsss Feb 27 '11 at 23:24
  • There are also options for serving pre-compressed files if you are using apache via mod_gzip (and still having the browser handle the decompression transparently) – Chris Farmiloe Feb 28 '11 at 00:01

5 Answers5

7

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.

Chris Farmiloe
  • 13,935
  • 5
  • 48
  • 57
  • This doesn't really apply for dynamic responses. Additional tricks are usually necessary to compress those. – Pekka Feb 27 '11 at 22:53
  • 1
    Not sure what makes you think that? Unless you are dealing with some kind of streaming app. Both apache and nginx will happily gzip up a dynamic response before returning it simply by adding the relevant line to enable the compression. – Chris Farmiloe Feb 27 '11 at 23:55
5

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

luksch
  • 11,497
  • 6
  • 38
  • 53
0

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);
Orwellophile
  • 13,235
  • 3
  • 69
  • 45
0

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/

Archie
  • 4,959
  • 1
  • 30
  • 36
0

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

Erik
  • 4,268
  • 5
  • 33
  • 49