Skip to content

Instantly share code, notes, and snippets.

@AlfieriChou
Created June 7, 2018 07:00
Show Gist options
  • Save AlfieriChou/33c6cf2b2c6ca41303824053809a7bdd to your computer and use it in GitHub Desktop.
Save AlfieriChou/33c6cf2b2c6ca41303824053809a7bdd to your computer and use it in GitHub Desktop.
文件打包
const path = require('path');
const express = require('express')
const app = express()
const archiver = require('archiver')
app.get('/', function(req, res) {
const archive = archiver('zip');
archive.on('error', function(err) {
res.status(500).send({error: err.message});
});
//on stream closed we can end the request
archive.on('end', function() {
console.log('Archive wrote %d bytes', archive.pointer());
});
//set the archive name
res.attachment('archive-name.zip');
//this is the streaming magic
archive.pipe(res);
const files = [__dirname + '/files/上午.png', __dirname + '/files/中午.json'];
for(const i in files) {
archive.file(files[i], { name: path.basename(files[i]) });
}
archive.finalize();
});
app.listen(3010)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment