Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unzip a .gz string in node.js

I am downloading a .csv.gz file from a remote server, and I have the contents of this file stored as a string. Here is a small sample of what I see when I console.log it:

�}�v������)��t�Y�j�8p0�eCR��

l��1�=���6������~̵r�����0c7�7L���������U:���0�����g��

How can I unzip this in Node.js so that it converts it to the original .csv file?

I have tried zlib.gunzip(Buffer.new(body), callback), but then I get an error

incorrect header check at Gunzip.zlibOnError (zlib.js:152:15)

The file itself is valid, and I can double-click to unzip and open it on my computer.

I create the file using: zlib.createGzip(); and then gzip.pipe(writeStream);


Update

The (actual) issue was my data was utf8 encoded so I needed to ensure that it remained either as a Buffer or binary.

like image 763
d-_-b Avatar asked Jul 04 '26 20:07

d-_-b


2 Answers

The problem is that fs.createWriteStream defaults to utf-8 encoding, you should change that to binary, then you'll be able to create a valid buffer that gunzip will happily accept.

You could probably accomplish this by changing your code to:

gzip.pipe(data => writeStream(data, { encoding: 'binary'})

see https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options

like image 193
Eelke Avatar answered Jul 06 '26 10:07

Eelke


UPDATE: I have modified the code so that now you have a ArrayBuffer which gets actually decompressed.

  function decompressFile(filename) {
       var decompress = zlib.createUnzip(),
        input = fs.createReadStream(filename);      
        var data = [];
        input.on('data', function(chunk){
            data.push(chunk);               
        }).on('end', function(){
            var buf = Buffer.concat(data);
            zlib.gunzip(buf, function(err, buffer) {
              if (!err) {
                console.log(buffer.toString()+'\n');
              }else{
                console.log(err);
              }
            });
        });
}
decompressFile('TestFileSheet1.csv.gz');

This looks straight forward. But I think the problem might be somewhere else in your code Or the http library that you are using. Check whether the response header's content encoding is gzip and then call the zlib.gunzip. I think your http library might already be decompressing the csv file.

like image 36
karthick Avatar answered Jul 06 '26 10:07

karthick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!