han0idx han0idx - 11 months ago 55
Node.js Question

Inflate a compressed with nodejs

Im trying to uncompress a file using Zlib library but finally seems uncompress only the first 8 Bytes, i search everyware but cant found a solution, how can i get the entire data ?

Here the code:

var zlib = require('zlib');
fs = require('fs')
fs.open('./file1.ff', 'r', function(status, fd) {
if (status) {
var bufferOrig = new Buffer(100);
fs.read(fd, bufferOrig, 0, bufferOrig.length, 76, function(err, num) {
zlib.inflate(bufferOrig, {
finishFlush: zlib.Z_SYNC_FLUSH
}, function(err, bufferDef) {
if (!err) {
fs.writeFile("./file2.ff", bufferDef, function(err) {
if (err) return console.log(err);
console.log("The file was saved!");
} else console.log('inflate error: ' + err);

Original Buffer:

Buffer 78 9c ec dd 07 5c 14 d7 da 07 e0 b3 4b 47 84 a5 59 a3 2e 0b a2 58 81 05 7b a1 da 75 7a 9f 11 a9 76 17 a9 36 58 54 2c a9 6a ec d1 18 8d dd 18 35 f6 96 ...

Inflated Buffer (only 8 bytes ??) :

Buffer f0 a0 f6 ff 26 4c 00 00

Answer Source

Deflate vs. gzip can be confusing so double check which one it is.

If you are just trying to gunzip or unzip files, maybe it would be simpler to use a module that does it in one step given the filenames. If it is a .gz file, something this simple may work:

require('gunzip-file')('example.json.gz', 'example.json');

If you actually need the control and its deflated but not gzipped, and want to use the zlib built into the current Node version, the explanation is near the top of the page https://nodejs.org/api/zlib.html#zlib_zlib

const inflate = zlib.createInflate();
const fs = require('fs');
const inp = fs.createReadStream('file1.ff');
const out = fs.createWriteStream('file1_inflated.ff');