Skip to content

Instantly share code, notes, and snippets.

@branneman
Last active July 30, 2021 20:59
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save branneman/e3deb4b91d78025a780917af514eecad to your computer and use it in GitHub Desktop.
Save branneman/e3deb4b91d78025a780917af514eecad to your computer and use it in GitHub Desktop.
Node.js script to create a zip file from a list of files and directories
const stat = require('fs').statSync;
const AdmZip = require('adm-zip');
/**
* Example usage
*/
newArchive(`test-${+new Date}.zip`, [
'index.js',
'package.json',
'node_modules'
]);
/**
* @param {String} zipFileName
* @param {Array<String>} pathNames
*/
function newArchive(zipFileName, pathNames) {
const zip = new AdmZip();
pathNames.forEach(path => {
const p = stat(path);
if (p.isFile()) {
zip.addLocalFile(path);
} else if (p.isDirectory()) {
zip.addLocalFolder(path, path);
}
});
zip.writeZip(zipFileName);
}
@pankaj-ve
Copy link

As per my requirement, I found the best answer for me at this link: https://jsonworld.wordpress.com/2019/09/07/how-to-zip-and-download-files-in-nodejs-express/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment