Last active
July 30, 2021 20:59
-
-
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
This file contains hidden or 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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/