Is there a way to use browser's native gzip decompression using Javascript?

Viewed 8867

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?

2 Answers

The Compression Streams API is a new web standard and is currently available in Chrome (since v80), Edge, and Deno. Other browsers will eventually add it, but in the mean time the best bet is a WASM implementation. Apparently WASM implementations can approach 90% of the performance of a native implementation (and ~20x the speed of a JS implementation).

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'));

More info:

Related