Search code examples
node.jszlibunzip

Node Zlib. Unzip response with file structure in-memory


I'm receiving a Buffer data from a response with data in a file.

// Save it just to check if content is correct
const fd = fs.openSync('data.zip', 'w')
fs.writeSync(fd, data)
fs.closeSync(fd)

Produces file data.zip, which contains file foo.csv.

I can unzip it with UZIP in-memory with:

const unzipArray = UZIP.parse(data)['foo.csv']

However, I cannot do it with Zlib.

const unzipArray = zlib.unzipSync(data)
// Rises: incorrect header check

It looks like Zlib cannot parse the file structure.

How to unzip the above buffer in-memory with Zlib, without saving files to the filesystem?


Solution

  • You have a zip file, not a zlib or gzip stream. As you found, zlib doesn't process zip files. There are many solutions out there for node.js, which you can find using your friend google. Here is one.