Anyway to unzip a file uploaded in memory using Multer?

青春壹個敷衍的年華 提交于 2021-01-29 20:23:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!