Skip to content

Instantly share code, notes, and snippets.

@MadLittleMods
Last active October 4, 2017 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MadLittleMods/72bc11761e05a2658d0be13fa8c27fef to your computer and use it in GitHub Desktop.
Save MadLittleMods/72bc11761e05a2658d0be13fa8c27fef to your computer and use it in GitHub Desktop.
Stream .zip of some glob (directory, etc). See .tar.gz equivalent: https://gist.github.com/MadLittleMods/7eedb4001c52acec104e91dbd80618b5
const Promise = require('bluebird');
const path = require('path');
const glob = Promise.promisify(require('glob'));
// Adds res.zip to express
require('express-zip');
const express = require('express');
const app = express();
app.get('/logs.zip', function (req, res) {
const logDirPath = path.join(process.cwd(), './logs/');
glob(path.join(logDirPath, '**/*'), { nodir: true })
.then((files) => {
const fileList = files.map((file) => {
return {
path: file,
name: path.relative(logDirPath, file),
};
});
res.zip(fileList, 'logs.zip');
})
.catch((err) => {
res
.status(500)
.set('Content-Type', 'text/plain')
.send('Error', err, err.stack);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment