Decompress Gzip and Zlib String in JavaScript

Decompress gzip and zlib string in javascript

I can solve my problem by zlib . I fix my code as below

var base64Data = "eJztwTEBAAAAwqD1T20JT6AAAHgaCWAAAQ==";
var compressData = atob(base64Data);
var compressData = compressData.split('').map(function(e) {
return e.charCodeAt(0);
});
var inflate = new Zlib.Inflate(compressData);
var output = inflate.decompress();

Decompress GZIP string response from a PHP server in NodeJS

This is the solution that works for me.

I used got instead of axios because I can't get it to work there.

I set my request options:

const requestOptions = {
encoding: null, // this is important
headers: {
"Accept-Encoding": "gzip",
}
...
};

Don't forget that encoding: null line, because without that, the response will be automatically converted to a string. ( We need a buffer to make this work )

Then I created a function like this to handle my request:

const zlib = require("zlib");

async function performRequest(url, options) {
try {
const response = await got(url, options);
if (response.headers["content-encoding"] === "gzip") {
const body = response.body;
try {
const dezziped = zlib.gunzipSync(response.body);
response.body = JSON.parse(dezziped.toString());
} catch (error) {
response.body = body;
}
}
return response.body;
} catch (error) {
return error;
}
}

Note: This is a synchronous operation, you can use gunzip instead if you want to do the async way.

Decompress gzip data without header using Node.JS zlib module

I think you want zlib.inflateRaw() and friends.

How to decompress image in JavaScript that was compressed by MS Sql

First, make sure you're getting a Base64 string as input to DecompressImage(). The problem might be that you're getting a hex string when you want an ArrayBuffer or a Base64 string. A hex string over the wire is even more inefficient than the uncompressed data. If you're sure that you're actually using a Base64 string from the JSON, you convert to a Uint8Array and decompress it.

Now that you've ensured you have a base64 input, note that the NPM zlib library is outdated and yields very low performance compared to modern alternatives like pako or fflate (my own library). As fflate is both smaller and faster than pako, I'll use it for this answer. Here's how you might go about decompressing the data:

// Install both fflate and base64-js for maximum speed
import * as b64 from 'base64-js';
import { gunzipSync } from 'fflate';
// This should replace DecompressImage
// Decompresses gzip + base64 to base64
function decompressToBase64(base64String) {
const bytes = b64.toByteArray(base64String);
const decompressed = gunzipSync(bytes);
return b64.fromByteArray(decompressed);
}

I want to decompress a GZIP string with JavaScript

The "H4sIAAAAAAAA//NIzcnJVyguSUzOzi9LLUrLyS/XUSjJSMzLLlZIyy9SSMwpT6wsVshIzSnIzEtXBACs78K6LwAAAA==" is a base64 encoded string, you first need to decode that into a buffer.

textEncoder.encode just encodes that base64 encoded string into a byte stream.

How to do that depend on whether you are in a browser or on nodejs.

node.js version

To convert the unzipped data to a string you further have use new TextDecoder().decode()

For node you will use Buffer.from(string, 'base64') to decode the base64 encoded string:

import { ungzip } from 'pako';

// decode the base64 encoded data
const gzipedData = Buffer.from("H4sIAAAAAAAA//NIzcnJVyguSUzOzi9LLUrLyS/XUSjJSMzLLlZIyy9SSMwpT6wsVshIzSnIzEtXBACs78K6LwAAAA==", "base64");

console.log('gzipeddata', gzipedData);
const ungzipedData = ungzip(gzipedData);

console.log('ungziped data', new TextDecoder().decode(ungzipedData));

browser version

In the browser, you have to use atob, and you need to convert the decoded data to an Uint8Array using e.g. Uint8Array.from.

The conversion I used was taken from Convert base64 string to ArrayBuffer, you might need to verify if that really works in all cases.

// decode the base64 encoded data
const gezipedData = atob("H4sIAAAAAAAA//NIzcnJVyguSUzOzi9LLUrLyS/XUSjJSMzLLlZIyy9SSMwpT6wsVshIzSnIzEtXBACs78K6LwAAAA==")
const gzipedDataArray = Uint8Array.from(gezipedData, c => c.charCodeAt(0))

console.log('gzipeddata', gzipedDataArray);
const ungzipedData = pako.ungzip(gzipedDataArray);

console.log('ungziped data', new TextDecoder().decode(ungzipedData));
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/2.0.4/pako.min.js"></script>

Compression and decompression of data using zlib in Nodejs

Update: Didn't realize there was a new built-in 'zlib' module in node 0.5. My answer below is for the 3rd party node-zlib module. Will update answer for the built-in version momentarily.

Update 2: Looks like there may be an issue with the built-in 'zlib'. The sample code in the docs doesn't work for me. The resulting file isn't gunzip'able (fails with "unexpected end of file" for me). Also, the API of that module isn't particularly well-suited for what you're trying to do. It's more for working with streams rather than buffers, whereas the node-zlib module has a simpler API that's easier to use for Buffers.


An example of deflating and inflating, using 3rd party node-zlib module:

// Load zlib and create a buffer to compress
var zlib = require('zlib');
var input = new Buffer('lorem ipsum dolor sit amet', 'utf8')

// What's 'input'?
//input
//<Buffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>

// Compress it
zlib.deflate(input)
//<SlowBuffer 78 9c cb c9 2f 4a cd 55 c8 2c 28 2e cd 55 48 c9 cf c9 2f 52 28 ce 2c 51 48 cc 4d 2d 01 00 87 15 09 e5>

// Compress it and convert to utf8 string, just for the heck of it
zlib.deflate(input).toString('utf8')
//'x???/J?U?,(.?UH???/R(?,QH?M-\u0001\u0000?\u0015\t?'

// Compress, then uncompress (get back what we started with)
zlib.inflate(zlib.deflate(input))
//<SlowBuffer 6c 6f 72 65 6d 20 69 70 73 75 6d 20 64 6f 6c 6f 72 20 73 69 74 20 61 6d 65 74>

// Again, and convert back to our initial string
zlib.inflate(zlib.deflate(input)).toString('utf8')
//'lorem ipsum dolor sit amet'

Python zlib gzip decompression fails with string compressed on client side via pako

Move the zlib_window_size from decompress() to decompressobj(). Or just get rid of the decompressobj(). You don't need it for a single call of decompress().



Related Topics



Leave a reply



Submit