Created
July 26, 2021 17:51
-
-
Save senthilmpro/3c375b66086ab7fd9d717c6babdd16c2 to your computer and use it in GitHub Desktop.
zip-directory-in-nodejs
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'); | |
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