-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a proper way to send zlib partial files for unzipping? #4030
Comments
I think you should not use |
Regarding the partial data behavior after #2595: decompression is done the same way as before. The only difference is that once you call You could ask |
cc @indutny and @trevnorris |
Yeah, I think you may want to write partial data and flush, should work just fine. |
Can you explain what you mean by "flush"? I'm relatively new to node streams. |
Sorry, I was referring to https://nodejs.org/api/zlib.html#zlib_zlib_flush_kind_callback |
Thanks for all the suggestions and help. Here's what ended up working for me.
example
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. Let me know if there are any consequences of this strategy I might not be aware of. |
Closing as this seems to have been answered. Thanks! |
I know this has already been closed, but PR #6069 makes it possible to do this synchronously too. The synchronous version of @constellates answer above is: var bufferSize = 500; // you'll need to account for the header size
var buffer = new Buffer(bufferSize);
var fd = fs.openSync(file.path, 'r');
fs.readSync(fd, buffer, 0, bufferSize, 0);
var outBuffer = zlib.unzipSync(buffer, {finishFlush: zlib.Z_SYNC_FLUSH});
console.log(outBuffer.toString()); I use it for reading the start of gzipped log files to parse the date of the first line (oldest entry). Source: |
Thanks @justinsg I appreciate hearing about the update. |
@justinsg How do you account for the header size?, what i understand is there is some book keeping bytes at start are those fixed? or is there a formulae for this? I know my application header size |
I saw a bug was fixed in v5.0.0 so that zlib will throw an error when it reaches the end of a truncated input (#2595). Is it possible to use zlib on partial files.
I'm working on a project that needs to be able quickly read through a large package of gzipped files (20+ files with a combined size of 2-5GB). All the content I care about is in the header of each file (first 500 bytes). I had this working previously using fs.read() while passing options to only read the first 500 bytes then using zlib.gunzip() to decompress the contents before parsing the header from the binary data.
This now throws an "unexpected end of file" input after v5.0.0. Is there another way to accomplish this or is zlib going to throw errors for the process regardless of what I do?
I've tried using streams and the chunks in the
.on('data')
event are being properly decompressed and parsed but I'm not confident the chunk size will always contain the full header and I still have to handle the error which is breaking the pipe before it gets to an "end" or "close" event.The text was updated successfully, but these errors were encountered: