ffxsam
ffxsam

Reputation: 27733

Node.js gunzip not working as expected

I have a base64-encoded file called enc. I can confirm that it's in gzip format via command line:

$ cat enc | base64 -D | gzcat

The uncompressed text displays fine.

However, this code fails to work:

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

const inp = fs.readFileSync('enc');
const buf = Buffer.from(inp, 'base64');
zlib.gunzip(buf, (err, buffer) => {
  console.log(err, buffer);
});

This error is thrown:

Error: incorrect header check
    at Zlib._handle.onerror (zlib.js:370:17) errno: -3, code: 'Z_DATA_ERROR'

I don't understand where I'm going wrong here.

Upvotes: 0

Views: 537

Answers (1)

Tanaike
Tanaike

Reputation: 201408

I thought that the data when the file is read may be the reason of the error. So how about a following modification?

From :

const inp = fs.readFileSync('enc');

console.log(err, buffer);

To :

const inp = fs.readFileSync('enc', 'utf-8');

console.log(err, buffer.toString());

If I misunderstand your question, I'm sorry.

Upvotes: 1

Related Questions