Express js – Zip and download files

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

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

DMCA.com Protection Status