问题
I am using Multer to take a file uploaded on a form and keep it in memory so that I can parse it and spit out some stuff, but is there a way I can take in a zip file and be able to unzip it and still keep the unzipped files in memory without writing to a disk? I know there is an npm module for node called 'unzip', but it seems to only be able to pull a file from a physical location, unless I am mistaken. I appreciate any insight!
const multer = require('multer');
const storage = multer.memoryStorage();
const upload = multer({ storage: storage, putSingleFilesInArray: true });
// landing page
router.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../../views', 'index.html'));
});
// parse multiple files from the form
router.post('/upload', upload.array('upload'), function (req, res) {
console.log(req.files);
});
回答1:
Solved using AdmZip npm package (shown below).
var AdmZip = require('adm-zip');
if (req.files[0].mimetype == 'application/x-zip-compressed'){
var zip = new AdmZip(req.files[0].buffer);
var zipEntries = zip.getEntries();
zipEntries.forEach(function(zipEntry) {
//do stuff
});
来源:https://stackoverflow.com/questions/52877912/anyway-to-unzip-a-file-uploaded-in-memory-using-multer