My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Post hidden from Hashnode

Posts can be hidden from Hashnode network for various reasons. Contact the moderators for more details.

How To Zip And Download Files In Nodejs

How To Zip And Download Files In Nodejs

Pankaj Kumar's photo
Pankaj Kumar
·Jun 21, 2022·

1 min read

In this article, We will see how to create zip in nodejs express application. Since many times we need to download zip file in our web application.

Its very easy to do with nodejs express. Have a look on the below code snippet.

const express = require('express')
const app = express()
const port = 3000;
const AdmZip = require('adm-zip');

app.get('/', (req, res) => {

const zip = new AdmZip();

zip.addLocalFile("./file1.txt");
zip.addLocalFile("./file2.js");
zip.addLocalFile("./file3.ts");

const downloadName = `${Date.now()}.zip`;
const data = zip.toBuffer();
res.set('Content-Type','application/octet-stream');
res.set('Content-Disposition',`attachment; filename=${downloadName}`);
res.set('Content-Length',data.length);
res.send(data);

})

app.listen(port, () => console.log(`Server is running over port ${port}!`))

Click here to find more about the adm-zip package.

So in this article, We learn how to create zip and download it with nodejs.

That’s all for now. Thank you for reading and I hope this artible will be very helpful to understand how to create zip and download it with nodejs.

This article was originally posted over jsonworld