Search code examples
javascriptzlibpako

Inflating partial gzip content with pako?


In node, zlib can be used to decompress partial gzip content (truncated). I tried the same with pako, but looks like it's not working.

This is what I tried:

const s = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8bV23J15O4\xb14\xb1H61417KKLL\xb50L5U\x8a\x05\x00\xf6\xaa\x8e.\x1c\x00\x00\x00";

const truncated = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x8bV23J15O4\xb14\xb1H61417KKLL\xb50L5U\x8a\x05\x00\xf6\xaa\x8e.\x1c\x00\x00";  // doesn't work

const array = Uint8Array.from([...s].map(v => v.charCodeAt(0)));

var data        = pako.inflate(array);

var strData2     = String.fromCharCode.apply(null, new Uint16Array(data));

console.log(strData2);

decompressing s works but when I try to decompress truncated i get an empty string.

Update:

tried also with the stream syntax, doesn't work:

const inflate = new pako.Inflate({ level: 3});
inflate.push(array, true);   // tried also false

Solution

  • Use const inflate = new pako.Inflate({chunkSize: 1});. That delivers every byte as it's decompressed to inflate.onData. Provide what you have using inflate.push(stuff, false). Then use inflate.onData to accumulate the bytes as they're generated.