I am still new to Javascript. I have a situation where many users can send large JSON back to the server. In order to limit traffic, I would like to gzip them. Is this possible in Javascript? How can I can create a byte array from the string representation of the JSON? Thanks.
            Asked
            
        
        
            Active
            
        
            Viewed 1.4k times
        
    2 Answers
3
            
            
        I know of no gzip implementations, but there are other compression methods at your disposal.
This will lzw-encode a string using JavaScript:
// lzw-encode a string
function 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("");
}
 
    
    
        Jason
        
- 3,379
- 25
- 32
- 
                    From: http://stackoverflow.com/questions/294297/javascript-implementation-of-gzip – Jason Nov 12 '11 at 05:54
0
            
            
        I believe so, here's a wikipedia article on the subject http://en.wikipedia.org/wiki/HTTP_compression
 
    
    
        Tom
        
- 43,583
- 4
- 41
- 61
