Is there a library available for compression in Javascript [closed]
Asked Answered
T

5

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.

Timothytimour answered 27/2, 2011 at 22:17 Comment(5)
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?Heidiheidie
Please note that HTTP/1.1 already supports three different compression formats ('gzip', 'deflate', and 'compress').Emalia
@gkaytck: Does anything #2349710 help?Habitat
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.Lockyer
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)Brisesoleil
B
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.

Brisesoleil answered 27/2, 2011 at 22:22 Comment(2)
This doesn't really apply for dynamic responses. Additional tricks are usually necessary to compress those.Heidiheidie
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.Brisesoleil
S
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

Sawyere answered 28/4, 2014 at 15:58 Comment(0)
I
0

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

Iambic answered 16/9, 2011 at 7:11 Comment(2)
-1, that library is broken, it returns "bytes" that are greater than 255.Burkle
Works fine for me, and other more developed (node based) solutions have been ported almost verbatim from that code, with no mention of issue.Irruption
I
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);
Irruption answered 11/6, 2013 at 10:32 Comment(0)
S
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/

Stancil answered 21/5, 2014 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.