-
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
ZLib support for decoding Zip files #26332
Comments
I don't see why that wouldn't work. Node.js checks for Z_STREAM_END and when it sees that status code, it resets the stream and starts inflating the remaining bytes in the current chunk. If you're saying that's not working for you, can you post a minimal standalone test case? Or do you mean you want Node.js to stop after the Z_STREAM_END without consuming the remaining bytes? |
Does checking It doesn’t quite work this way for gunzip, because those streams try to continue reading after EOF has been reached, in case another member of the gzip file follows. But I don’t think that’s what you’re looking for anyway? |
I was looking for a way to know when the stream terminated without being otherwise told by one of the zip headers and I couldn't figure it out. :( So maybe
Is there an example that determines when the stream terminates using Also, could you point me to where Node.js checks for Thanks in advance for any pointers! |
Ah! Here is where it node.js checks for while (strm_.avail_in > 0 &&
mode_ == GUNZIP &&
err_ == Z_STREAM_END &&
strm_.next_in[0] != 0x00) {
// Bytes remain in input buffer. Perhaps this is another compressed
// member in the same archive, or just trailing garbage.
// Trailing zero bytes are okay, though, since they are frequently
// used for padding.
ResetStream();
err_ = inflate(&strm_, flush_);
} And it looks like it's only applied in GUNZIP mode. Eh. But here's
That's looking good... except, like the docs say, that's for #define GZIP_HEADER_ID1 0x1f
#define GZIP_HEADER_ID2 0x8b But let's see how this Mr. Adler points us to this as the reference implementation of |
var zlib = require('zlib');
var assert = require('assert');
const input = Buffer.from('01234567890123456789');
zlib.deflate(input, (err, deflatedBuffer) => {
assert(!err);
deflatedBuffer = Buffer.concat([deflatedBuffer, Buffer.from([1,2,3,4,5,6])]);
var numberRead = 0;
var buffers = [];
var stream = zlib.createInflate()
.on('data', function(chunk) {
buffers.push(chunk);
numberRead += chunk.length;
})
.on('end', function() {
var result = Buffer.concat(buffers, numberRead);
this.close();
console.log(result.toString());
console.log(stream.bytesWritten);
})
.end(deflatedBuffer);
}); Returns
So |
@kingces95 Glad you got something that works for you! That being said, I think it might be a bug that the zlib stream doesn’t emit the (That’s something you’d have to work around when supporting all existing Node.js versions, though.) |
Report end-of-stream when decompressing when we detect it, and do not wait until the writable side of a zlib stream is closed as well. Refs: nodejs#26332
(See #26363 for a patch that should help here.) |
Report end-of-stream when decompressing when we detect it, and do not wait until the writable side of a zlib stream is closed as well. Refs: nodejs#26332 PR-URL: nodejs#26363 Refs: nodejs#26332 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
Report end-of-stream when decompressing when we detect it, and do not wait until the writable side of a zlib stream is closed as well. Refs: #26332 PR-URL: #26363 Refs: #26332 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
I'd like to write a zip file decompressor in node. To do that, I'd need to be able to inflate a deflated stream without knowing it's length in advance (some zip files do not include the deflated stream's length until after the stream).
I'm not entirely sure, but I don't think node currently allows this. The popular npm unzip modules all search for a magic signature to find the end of the deflate stream. This is incorrect (per Mark Adler) as the magic signature might appear as compressed data in the deflate stream itself. I've linked to some of those examples in the SO discussion with Mark Adler on SO here.
I think node strives to expose the deflate logic as a stream transform. A stream transform expects to to consume each of the data chunks passed to it. So there's no way, via that abstraction, to have the stream self report it's own termination mid chunk.
I believe this is the reference implementation: https://github.com/madler/zlib/blob/master/examples/gzappend.c
The text was updated successfully, but these errors were encountered: