Last active
August 1, 2019 08:59
-
-
Save ahmadawais/f6c920e440551bbaab52f0e9cd1aba04 to your computer and use it in GitHub Desktop.
Zip a directory with JavaScript in Node.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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