3

I'm getting gzipped data from a business api i'm working with, but I can't manage to decompress it into something readable in JS, though I managed with C#.

My question is - how do I unzip the received gzipped input to a string or json?

The following code works well for me in C#:

using (HttpWebResponse response = (HttpWebResponse)WebRequest.Create(url).GetResponse())
{
    using (GZipStream decompress = new GZipStream(response.GetResponseStream(), CompressionMode.Decompress))
    {
        using (StreamReader reader = new StreamReader(decompress))
        {
            responseFromServer = reader.ReadToEnd();
        }
    }
}

I've read various answers and tried some libraries but still can't manage to decompress it in JS (using same URL).

This is where the code should be in JS:

var requestData = {
    url: url,
    headers: {
        "Allow-Encoding": "gzip"
    }
}
request.get(requestData, function(error, response, body) {

    // compressed data is in body
});

I've tried pako, zlib but I am probably not using them correctly.

[edit] Some of my tries:

// Decode base64 (convert ascii to binary)
var strData = new Buffer(body).toString('base64');

// Convert binary string to character-number array
var charData = strData.split('').map(function (x) { return x.charCodeAt(0); });

// Turn number array into byte-array
var binData = new Uint8Array(charData);

// Pako magic
var data = pako.inflate(binData);

// Convert gunzipped byteArray back to ascii string:
var strData2 = String.fromCharCode.apply(null, new Uint8Array(data));

This code is running in a NodeJS application, and i'm using request package

Thanks

AlexD
  • 4,062
  • 5
  • 38
  • 65
  • You can try https://github.com/beatgammit/gzip-js – jcubic Jul 26 '15 at 12:56
  • I checked that library, but it compresses not decompresses – AlexD Jul 26 '15 at 12:58
  • if you look at the source code there is unzip function. – jcubic Jul 26 '15 at 12:59
  • @AlexD - what is your question? – enhzflep Jul 26 '15 at 13:02
  • How do I unzip the body to a string ? – AlexD Jul 26 '15 at 13:05
  • @enhzflep tried it, it keeps saying my file is not a gzip file. Perhaps I need to do some formatting before I call unzip? If so then what exactly? – AlexD Jul 26 '15 at 13:26
  • @AlexD - was just wondering if that might be happening. I seem to have found the SO post and associated fiddle that the snippets in your post are based on, and am currently looking at the result of atob on the fiddle's data (a b64-encoded, gzipped version of the text 'Looks like we've got ourselves a decoded string!) - I wonder if your first line is wrong - I'd be trying with `atob`, i.e `var strData = atob(body);` - Is the length of `strData` shorter than the length of `body`? If so, perhaps you're base64 encoding an already base64 encoded string. – enhzflep Jul 26 '15 at 13:36
  • I tried switching atob to a different function, since atob seems to be a window function, and since I'm running in NodeJS its unavailable? Or perhaps I didn't quiet understand what you meant, can you elaborate? perhaps with some code example I should test? Thanks! – AlexD Jul 26 '15 at 13:40
  • @AlexD - oh yes, of course. Sorry, I forgot you were using Node. I don't use it, and can only offer the following post: http://stackoverflow.com/questions/8305988/convert-binary-tostringencode64-back-to-binary – enhzflep Jul 26 '15 at 13:52
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84305/discussion-between-alexd-and-enhzflep). – AlexD Jul 26 '15 at 14:41

0 Answers0