Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created July 26, 2021 17:51
Show Gist options
  • Save senthilmpro/3c375b66086ab7fd9d717c6babdd16c2 to your computer and use it in GitHub Desktop.
Save senthilmpro/3c375b66086ab7fd9d717c6babdd16c2 to your computer and use it in GitHub Desktop.
zip-directory-in-nodejs
const fs = require('fs');
const archiver = require('archiver');
const path = require('path');
// inspired from https://stackoverflow.com/questions/15641243/need-to-zip-an-entire-directory-using-node-js
const zipDirectory = (dirPath, outputFile = "target.zip") => {
const dPath = path.resolve(dirPath);
var output = fs.createWriteStream(outputFile);
const archive = archiver('zip');
output.on('close', () => console.log(`Archiver completed. Total Bytes: ${archive.pointer()}`));
output.on('error', (err) => { throw err });
archive.pipe(output);
archive.directory(dPath, false);
archive.finalize();
}
module.exports = {
zipDirectory
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment