Skip to content
Advertisement

I want to decompress a GZIP string with JavaScript

I have this GZIPed string: H4sIAAAAAAAA//NIzcnJVyguSUzOzi9LLUrLyS/XUSjJSMzLLlZIyy9SSMwpT6wsVshIzSnIzEtXBACs78K6LwAAAA==

I created that with this website: http://www.txtwizard.net/compression

I have tried using pako to ungzip it.

JavaScript

The issue is that Pako throws the error: incorrect header check

What am I missing here?

A JSbin

Advertisement

Answer

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:

JavaScript

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.

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement