Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Last active August 1, 2019 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/f6c920e440551bbaab52f0e9cd1aba04 to your computer and use it in GitHub Desktop.
Save ahmadawais/f6c920e440551bbaab52f0e9cd1aba04 to your computer and use it in GitHub Desktop.
Zip a directory with JavaScript in Node.js.
const fs = require('fs');
const archiver = require('archiver');
/**
* Zip a directory.
*
* @param {String} src Source directory path.
* @param {String} dst Destination path + filename.zip.
*/
module.exports = async (src, dst) => {
const archive = archiver.create('zip', {});
const output = fs.createWriteStream(dst);
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.log(err);
} else {
throw err;
}
});
archive.on('error', function(err) {
throw err;
});
await archive.pipe(output);
await archive.directory(src, false);
await archive.finalize();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment