23

I am using a lot of ajax calls to query the database and I get large text (json) responses. I will like to compress the response.

There is a great way of compressing text using javascript in JavaScript implementation of Gzip.

The problem is I want to compress the response on my aspx server and decmpress it with javascript. Therefore I need to run the lzw_encode function on my asp.net server. Should I I translate that function to C# or there is another way?

Using the link above if you dont want to configure IIS or change header you can compress the code on the server with:

C#

    public static string Compress(string s)
    {
        var dict = new Dictionary<string, int>();
        char[] data = s.ToArray();
        var output = new List<char>();
        char currChar;
        string phrase = data[0].ToString();
        int code = 256;

        for (var i = 1; i < data.Length; i++){
            currChar = data[i];
            var temp = phrase + currChar;
            if (dict.ContainsKey(temp))                
                phrase += currChar;                
            else
            {
                if (phrase.Length > 1)                    
                    output.Add((char)dict[phrase]);                    
                else                    
                    output.Add((char)phrase[0]);                    
                dict[phrase + currChar] = code;
                code++;
                phrase = currChar.ToString();
            }
        }

        if (phrase.Length > 1)            
            output.Add((char)dict[phrase]);            
        else            
            output.Add((char)phrase[0]);

        return new string(output.ToArray());
    }

and on the browser you can decompress it with:

javascript

// Decompress an 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;
    for (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("");
} 
Community
  • 1
  • 1
Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 9
    The easiest approach to compress the JSON (and any other) response is to ensure your web server is using HTTP compression. http://en.wikipedia.org/wiki/HTTP_compression The link you refer to in your question talks about compressing data in JavaScript so that it takes up less storage space when it is stored on the server. – Eric J. Oct 23 '13 at 18:14
  • Yeah that is probably a more elegant solution. I will give it a try thanks! – Tono Nam Oct 23 '13 at 18:17
  • @TonoNam - I've changed the question to match your actual problem. Feel free to revert/edit. – Alexei Levenkov Oct 23 '13 at 18:22
  • Thanks sounds much better now! – Tono Nam Oct 23 '13 at 18:23

1 Answers1

65

Within your server-side response object add a header for GZip, like this:

Response.AddHeader("Content-Encoding", "gzip");

Also, you can use the GZipStream class to compress your content, like this:

Response.Filter = new GZipStream(Response.Filter,
                                 CompressionMode.Compress);
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • 1
    This is **SUPER** useful, I wish I could give this another +100. – Aviad P. Nov 14 '14 at 18:02
  • Can I use if I use JsonResult method: return Json(players, JsonRequestBehavior.AllowGet); – VAAA Jun 13 '16 at 00:17
  • 1
    Karl I also want more details on how to use `Response.AddHeader` same case as @VAAA trying to make my `return Json` send the data compress – Juan Carlos Oropeza Aug 22 '16 at 19:39
  • 1
    This answer was related to solving this issue with ASP.NET WebForms, but I will point you to an accepted answer for [how to compress content in ASP.NET MVC](http://stackoverflow.com/questions/3802107/how-to-gzip-content-in-asp-net-mvc) – Karl Anderson Aug 22 '16 at 19:49