Skip to content

Instantly share code, notes, and snippets.

@aztack
Last active August 1, 2022 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/c61462f1ad166768dbe0fc52f7ed3103 to your computer and use it in GitHub Desktop.
Save aztack/c61462f1ad166768dbe0fc52f7ed3103 to your computer and use it in GitHub Desktop.
Express.js Download Streamed Zip
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