How can I avoid zlib "unexpected end of file" when GUnzipping partial files?
Thanks for all the suggestions. I also submitted a question issue to the node repository and got some good feedback. Here's what ended up working for me.
- Set the chunk size to the full header size.
- Write the single chunk to the decompress stream and immediately pause the stream.
- Handle the decompressed chunk.
example
var bytesRead = 500;
var decompressStream = zlib.createGunzip()
.on('data', function (chunk) {
parseHeader(chunk);
decompressStream.pause();
}).on('error', function(err) {
handleGunzipError(err, file, chunk);
});
fs.createReadStream(file.path, {start: 0, end: bytesRead, chunkSize: bytesRead + 1})
.on('data', function (chunk) {
decompressStream.write(chunk);
});
This has been working so far and also allows me to keep handling all other gunzip errors as the pause() prevents the decompress stream from throwing the "unexpected end of file" error.