Miroslav Popov
Miroslav Popov

Reputation: 3383

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?

Upvotes: 0

Views: 3634

Answers (2)

Mark Adler
Mark Adler

Reputation: 112384

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.

Upvotes: 2

Marco Bertelli
Marco Bertelli

Reputation: 425

for single file:

const fs = require('fs');
const zlib = require('zlib');

const fileContents = fs.createReadStream('./data/file1.txt.gz');
const writeStream = fs.createWriteStream('./data/file1.txt');
const unzip = zlib.createGunzip();

fileContents.pipe(unzip).pipe(writeStream);

for a group of file:

const fs = require('fs');
const zlib = require('zlib');

const directoryFiles = fs.readdirSync('./data');

directoryFiles.forEach(filename => {
  const fileContents = fs.createReadStream(`./data/${filename}`);
  const writeStream = fs.createWriteStream(`./data/${filename.slice(0, -3)}`);
  const unzip = zlib.createGunzip();
  fileContents.pipe(unzip).pipe(writeStream);
});

Upvotes: -1

Related Questions