Skip to content

Is there a proper way to send zlib partial files for unzipping? #4030

@constellates

Description

@constellates

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.

var readStream = fs.createReadStream(file.path, {start: 0, end: 500});
var gunzip = zlib.createGunzip();

readStream.pipe(gunzip)
    .on('data', function(chunk) {
        console.log(parseBinaryHeader(chunk));
        console.log('got %d bytes of data', chunk.length);
    })
    .on('error', function (err) {
        console.log(err);
    })
    .on('end', function() {
        console.log('end');
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.zlibIssues and PRs related to the zlib subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions