简体   繁体   中英

Anyway to unzip a file uploaded in memory using Multer?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM