Last active
August 1, 2022 02:05
-
-
Save aztack/c61462f1ad166768dbe0fc52f7ed3103 to your computer and use it in GitHub Desktop.
Express.js Download Streamed Zip
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
import express from 'express'; | |
import archiver from 'archiver'; | |
express().use('/download-assets', async (req, res) => { | |
res.set('Content-Type', 'application/octet-stream'); | |
res.set('Transfer-Encoding', 'chunked'); | |
res.attachment(`assets.zip`); | |
console.log(`Archiving project assets...`); | |
const zipfile = archiver('zip'); | |
zipfile.on('error', (err: any) => { | |
console.log(`zip error: ${err}`); | |
res.status(500).json({ msg: `download assets failed: ${err}` }); | |
}); | |
zipfile.on('end', () => { | |
console.log('Done'); | |
res.status(200).end(); | |
}); | |
zipfile.on('entry', (e: { name: any }) => { | |
process.stdout.write(`\rarchiving ${e.name}${' '.repeat(5)}`); | |
}); | |
zipfile.pipe(res, { end: true }); | |
zipfile.directory(this.projectRoot, false); | |
zipfile.finalize(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment