How to extract zip from client in node

会有一股神秘感。 提交于 2021-02-07 10:54:58

问题


I'm having a node app which needs to get some zip file from client Postman and extract it to a folder in my fileSystem,Im using express I did the following which doesnt work,

what am I missing here?

I've created sample node app to simulate the issue.

var express = require('express');
var upload = require('multer')({ dest: 'uploads/' });
var admZip = require('adm-zip');
var app = express();

app.post('/',upload.single('file'),function(req,res){
    debugger;
    var zip = new admZip(req.file);
    zip.extractAllTo("C://TestFolder//TestPathtoExtract", true);
    res.send("unzip");

});

var server = app.listen(3001,function(){
    var host = server.address().address;
    var port = server.address().port;
    console.log('Example app listening at http://%s:%s',host,port);
})

This is how I use it im postman

If there is other way to do it with different open source this can be great! I use https://github.com/cthackers/adm-zip

which can be change to any other library

I've also find this lib but not sure how to use it with express https://www.npmjs.com/package/decompress-zip

Thanks!


回答1:


This is the set up I did for Postman, first this is my form-data body

Now in the header I left in blank after trying to set multipart/form-data manually and utterly failed, so no header here.

Here I did a pair of console.log, one of req.headers to be sure of Postman sending the right multipart/form-data and another of req.file

And well the output seems to be fine

Edit: the code.

var express = require('express');
var upload = require('multer')({
  dest: 'uploads/'
});
var admZip = require('adm-zip');
var app = express();

app.post('/', upload.single('file'), function(req, res) {
  console.log('%c > req.headers test.js [9] <=================================', 'color:blue;', req.headers);
  debugger;
  console.log('%c > req.file test.js [10] <=================================', 'color:blue;', req.file);
  //instead of just req.file I use req.file.path as admzip needs the actual file path
  var zip = new admZip(req.file.path);
  zip.extractAllTo("/Users/myuser/Desktop/ext", true);
  res.send("unzip");

});

var server = app.listen(3001, function() {
  var host = server.address().address;
  var port = server.address().port;
  console.log('Example app listening at http://%s:%s', host, port);
});



回答2:


You need to pass filename as argument.

Use req.file.path

  var zip = new admZip(req.file.path);


来源:https://stackoverflow.com/questions/34043569/how-to-extract-zip-from-client-in-node

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