1

I need to zip and unzip file with Node.js but I have a problem.

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



function UnZip(zip, paths) {

  var inp = fs.createReadStream("f:/test.zip");

  var Exzip = zlib.createUnzip();

  inp.pipe(Exzip).pipe("f:/");

}

Error:

TypeError: dest.on is not a function

Charlie
  • 18,636
  • 7
  • 49
  • 75
Mahdi_Ush
  • 21
  • 1
  • 6

2 Answers2

0

Zipping the file

const archiver = require('archiver'),
    archive  = archiver('zip'),
      fs       = require('fs'),
    output = fs.createWriteStream( 'mocks.zip');

archive.pipe(output);

// temp.txt file must be available in your folder where you 
// are writting the code or you can give the whole path

const file_buffer = fs.readFileSync('temp.txt')


archive.append(file_buffer, { name: 'tttt.txt'});


archive.finalize().then((err, bytes) => {
  if (err) {
    throw err;
  }
  console.log(err + bytes + ' total bytes');
});

unzipping a file

const unzip = require('unzip'),
      fs = require('fs');

fs.createReadStream('temp1.zip').pipe(unzip.Extract({ path: 'path' }))
Nayan Patel
  • 1,377
  • 18
  • 26
0

Here is how you can do it with zlib module.

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

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

fileContents.pipe(unzip).pipe(writeStream);
Charlie
  • 18,636
  • 7
  • 49
  • 75