Last active
January 24, 2023 11:59
-
-
Save alibo/6df912099f7729ba2ab6 to your computer and use it in GitHub Desktop.
Generating & Downloading zip folder on the fly (dynamically) using 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
//@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