Sending a streaming zip file in node.js

The webapp generator that we set up is now up and running on a heroku instance and is working to generate zips of generated websites.

One of the requirements was that when the front-end presses, the “Generate” button, a request is created to the backend node app to start creating the html pages, and pack them into a zip file. Then the said zip file will be made available to download.

I intended for the zip file to become available to download immediately and the server to continuously pack data into the zip, as the user downloads it – a process known as streaming zip serving. (If you might have noticed, Google Drive uses this too. You do not know the final zip size before the download has completed.)

If the server takes time in creating the contents of the zip, this method can help, as it will allow the user to start downloading the file, before all the contents are finalised.


To achieve the mentioned goal, the idea Node.js module to use archiver.

If you look at the app.js code you’ll find on the /generate POST endpoint, we take the ‘req’ params and pass to a pipeToRes() function, which is defined in generator.js'/generate', uploadedFiles, function(req, res, next) {
res.setHeader('Content-Type', 'application/zip');
generator.pipeZipToRes(req, res);

And in the generator, you can find this block of code that pipes the streaming archive to the response.


      const zipfile = archiver('zip');

      zipfile.on('error', (err) => {
        throw err;

      zipfile.pipe(res);, '/').finalize();

We zip the directory defined in distPath and the archiver API has a fluent call to .finalize() that finished the download for the user, when the zip is completed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.