Unzip the file does not work

Im using the following code from

https://github.com/cthackers/adm-zip/wiki/ADM-ZIP-Introduction

I need to get the zip file from the request (I use express , and I have the request and answer), and I need to extract (unzip) it to some path (in the example for my local disk), where I have to put req and Im not here to make it work.

fn: function (req, res) { var admZip = require('adm-zip'); var zip = new admZip(); zip.addLocalFile("C://TestFolder//TestZip"); 

in the body of the im request receiving the zip file (im using postman and in the body I use the binary and select the zip file)

+6
source share
2 answers

Please try my fragment code:

For some information, the structure of my application is as shown below:

 my path --> C:\xampp\htdocs\service service | -- tmp\ | -- app.js | -- index.html 

Client side:

 <html> <body> <h3>ZIP Upload:</h3> <form action="/upload_zip" method="POST" enctype="multipart/form-data"> Select zip to upload: <input type="file" name="zipFile" id="zipFile"> <input type="submit" value="Upload ZIP" name="submit"> </form> </body> </html> 

Server side:

Remember to use enctype="multipart/form-data" when you publish it using the mail manager or something like that ...

 var express = require("express"); var fs = require("fs"); var AdmZip = require('adm-zip'); var app = express(); var multer = require("multer"); var multer_dest = multer({dest: "./tmp"}).single('zipFile'); app.get("/",function(req,res){ console.log("Show index.html"); res.sendFile(__dirname+"/"+"index.html"); }); app.post("/upload_zip",multer_dest,function(req,res){ console.log(req.file); var zip = new AdmZip(req.file.path); zip.extractAllTo("./tmp"); result = { file:req.file, message:"File has been extracted" }; fs.unlink(req.file.path, function (e) { if (e) throw e; console.log('successfully deleted '+req.file.path); }); res.end(JSON.stringify(result)); }); var server = app.listen(8081,function(){ var host = server.address().address; var port = server.address().port; console.log("Example App Listening at http://%s:%s",host,port); }) 

Output:

enter image description here

0
source

You can simplify the problem by using form-data instead of binary and using multer . You can get the input file by contacting req.file , after which you can perform the unpack operation.

For example, you would add to your route:

 var upload = require('multer')({ dest: 'uploads/' }); var admZip = require('adm-zip'); app.post('/upload-here', upload.single('file'), function (req, res, next) { var zip = new admZip(req.file.path); zip.extractAllTo("C://TestFolder//TestZip", true); }); 
0
source

All Articles