How to have a browser gunzip an Ajax fetched gziped text file?

Jérôme Verstrynge picture Jérôme Verstrynge · Feb 13, 2013 · Viewed 7.5k times · Source

Assuming a server which cannot zip responses to user requests. A web developer nevertheless creates a myfile.txt.gz and stores it at http://www.mysite.com/myfile.txt.gz.

It is possible to have the browser automatically gunzip this compressed text file as part of the Ajax request and store the result in a var? If yes, how?

I am open to other compression algorithms if necessary.

Update

I am trying to use the following JQuery Ajax call:

var fetch = function() {

    $.ajax({
        type: 'GET',
        url:  "./data.txt.gz",
        headers: { "Accept-Encoding" : "gzip" },
        dataType: "text",
        async: true,
        success: function(result) {
            $("#midEnglob").text(result);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert("Issue: "
                + textStatus + " "
                + errorThrown + " !");
        }

    });

}

but I get the following in my browser:

enter image description here

Any ideas?

Answer

Christian picture Christian · Feb 13, 2013

letting the browser uncompress it with Content-Encoding: gzip is probably the best. In case it doesn't work for your scenario, there are many LZW implementations in javascript like: https://gist.github.com/revolunet/843889

you might have to try out different implementations, i didn't check any of them myself.