32

The backend server responds with a gzip file but without the Content-Encoding: gzip header. And I do not have control over the server so can't handle the issue server side.

What I need now is to decompress the gzipped file client side using javascript.

I have found this excellent library which can help me do this: http://nodeca.github.io/pako/

But I don't want to add additional library just to un-gzip a file. I feel that there should be a way to use the browser's native functionality to un-gzip. Am I correct? If I am wrong can someone explain why this browser functionality is not exposed as a javascript API? And is there a way to un-gzip a file in javascript without adding an additional library?

4
  • 2
    As far as I know you can't force a browser to un-gzip a page with javascript since you don't control how the browser renders the page. Commented Mar 23, 2016 at 17:44
  • 1
    I make a get ajax request to fetch the file Commented Mar 23, 2016 at 18:26
  • That's all good but if the server is not outputting the headers that match the content-encoding I believe you will have to manually un-gzip. Commented Mar 23, 2016 at 18:27
  • 1
    I think the question is exactly about manually un-gzip, a resource fetched via ajax request not an entire page Commented Sep 7, 2016 at 14:00

1 Answer 1

29

The Compression Streams API is a new web standard and is currently available in Chromium, Firefox, Safari, and Deno.

Some example usage of the Compression Streams API:

async function decompressBlob(blob) { let ds = new DecompressionStream("gzip"); let decompressedStream = blob.stream().pipeThrough(ds); return await new Response(decompressedStream).blob(); } 

And for compression:

const compressedReadableStream = inputReadableStream.pipeThrough(new CompressionStream('gzip')); 

You can also use a WASM implementation. Apparently WASM implementations can approach 90% of the performance of a native implementation (and ~20x the speed of a JS implementation).

See also:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped me a ton. Also, depending on your application, you might be able to convert directly without making a blob. What worked for me: let ds = new DecompressionStream('gzip'); let decompressed_stream = response.body.pipeThrough(ds); let decompressed_text = await new Response(decompressed_stream).text();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.