Skip to content

Instantly share code, notes, and snippets.

@alibo
Last active January 24, 2023 11:59
Show Gist options
  • Save alibo/6df912099f7729ba2ab6 to your computer and use it in GitHub Desktop.
Save alibo/6df912099f7729ba2ab6 to your computer and use it in GitHub Desktop.
Generating & Downloading zip folder on the fly (dynamically) using Node.JS
//@see https://www.npmjs.com/package/archiver
var archiver = require('archiver')
var http = require('http');
http.createServer(function(request, response) {
var archiver = archiver('zip')
archiver.pipe(response)
response.setHeader('Content-type', 'application/zip')
response.setHeader('Content-disposition', 'attachment; filename=file_name.zip');
archiver.bulk([{
expand: true,
cwd: "/path/to/folder", // source folder
src: ["**/*"], //globbing pattern
dot: true
}]);
archiver.finalize(function(err) {
if (err) res.send(500)
})
}).listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment