I need to decompress Base64-encoded gzip
or deflate
data from JavaScript without adding much to the bundle size (this is why I can't just use pako
). I found a nice, small library for decompressing deflate
data called UZIP
.
It appears that gzip
is just deflate
with a 10-byte header and an 8-byte footer. The data is guaranteed not to not have any corruption and not have any extra headers beyond those ten bytes. With all this in mind, I assumed that I could just Base64 decode the data, convert it to a Uint8Array
, and slice off the header and footer.
const data = // some Base64-encoded, gzipped string
const dataBytes = new Uint8Array(atob(data).split('').map(char => char.charCodeAt(0)));
const decompressed = UZIP.inflate(dataBytes.slice(10, -8));
This code hangs indefinitely (presumably because UZIP
has bad error handling). I tried using pako
and zlib
on both Node.js and the browser, but both complained about an invalid header. For reference, using zlib.gunzip
after decoding from Base64, as well as running pako.inflate
(which automatically detects the compression type), both properly decompress the gzipped data.
Is it possible to extract the raw deflate
data from a gzip string? If not, are there any lightweight libraries that can decompress both gzip
and deflate
?
I found that the deflate
spec does not include headers, but zlib
does, and many libraries call zlib
decompression inflate
, while calling true inflation inflateRaw
. I changed my method calls from inflate
to inflateRaw
and it immediately worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.