Posted by Arjun on Friday 14th December 2018

Express js - Zip and download files

How to Zip Download

<code>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}!`))</code>