Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decompress gzip files in Node.js

Tags:

node.js

gzip

I want to unzip gzip files in Node.js I've tried [some] packages but nothing is working. Can you provide a package with sample code which can decompress gzip files in Node.js?

like image 777
Malik Kamran Abid Avatar asked Aug 07 '17 16:08

Malik Kamran Abid


2 Answers

gunzip-file node package worked fine!

Do:

npm install gunzip-file

Then:

'use strict'

const gunzip = require('gunzip-file')

// 'sitemap.xml.gz' - source file
// 'sitemap.xml'    - destination file
// () => { ... }    - notification callback
gunzip('sitemap.xml.gz', 'sitemap.xml', () => {
  console.log('gunzip done!')
})

Finally, run with Node at your shell.

like image 51
ranieribt Avatar answered Oct 19 '22 07:10

ranieribt


I would suggest to use zlib.Gunzip.

Function prototype is zlib.Gunzip(buf, callback). The first argument is the raw archive data as a buffer that you want to extract, the second one is a callback which accept two arguments (result and error).

An implementation would be:

zlib.Gunzip(raw_data, function (error, result) {
   if (error) throw error;
   // Access data here through result as a Buffer
})
like image 38
Antoine C. Avatar answered Oct 19 '22 06:10

Antoine C.