MadLittleMods
10/4/2017 - 6:17 AM

Stream .zip of some glob (directory, etc). See .tar.gz equivalent: https://gist.github.com/MadLittleMods/7eedb4001c52acec104e91dbd80618b5

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);
    });
});