Skip to content

Express js – Zip and download files

Last updated on November 21, 2022

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

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

    const zip = new AdmZip();

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

    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(`Example app listening on port ${port}!`))
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments