9

I have node app with express and im sending request from client like postman and I need to get the file from the req and extract it on my local folder,how I can do that ?

I found the following open source but not sure how to take the req body and extract it in my local folder like "C://Test//extractDest",

The code below is taken from the following open source but if there is other good open source for this I can use it instead https://www.npmjs.com/package/decompress-zip

var DecompressZip = require('decompress-zip');
var unzipper = new DecompressZip(filename)

unzipper.on('error', function (err) {
    console.log('Caught an error');
});

unzipper.on('extract', function (log) {
    console.log('Finished extracting');
});

unzipper.on('progress', function (fileIndex, fileCount) {
    console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
});

unzipper.extract({
    path: "C://Test//extractDest",
    filter: function (file) {
        return file.type !== "SymbolicLink";
    }
});

This is how I send the zip file I simply select binary and choose a zip file

enter image description here

3
  • This might help you stuk.github.io/jszip/documentation/howto/read_zip.html
    – vinayr
    Dec 1, 2015 at 9:12
  • @vinayr - Thanks but how should I use it with express?
    – user4445419
    Dec 3, 2015 at 7:40
  • would you be interested in using a web based unzip? something like the cloud convert api it will have more capability then many of the javascript based unzip modules
    – Binvention
    Dec 7, 2015 at 23:36

2 Answers 2

9
+50

I'd recommend you using multer which works with multipart/form-data content-type.

Here's a basic working example. It assumes we are only uploading a single file, a folder named "uploads" exists at the root of your project and a form field named "singleFileUpload" to hold your data. You can change all those following the multer documentation:

var path     = require("path");
var express  = require("express");
var multer   = require("multer");
var Unzipper = require("decompress-zip");


var app = express();

app.use(multer({dest:'./uploads/'}).single('singleFileUpload'));


app.post("/", function(req, res){

    if (req.file){

        var filepath = path.join(req.file.destination, req.file.filename);
        var unzipper = new Unzipper(filepath);

        unzipper.on("extract", function () {
            console.log("Finished extracting");
        });

        unzipper.extract({ path: "C://Test//extractDest"});
    }

    res.status(204).end();
});


app.listen(3000);

Using postman you can now upload and decompress files with this configuration:

enter image description here

2
  • Thanks 1+ just one clarification what is the reason that we need the path var filepath = path.join(req.file.destination, req.file.filename);?
    – user4445419
    Dec 8, 2015 at 9:34
  • Sure. req.file.destination is the folder path of the uploaded file (./uploads/ in your case), and req.file.filename is the name of the file (multer generates the name for you). You need to combine both for decompress-zip to know where the file to decompress is located.
    – cviejo
    Dec 8, 2015 at 9:45
5

You can use multer and uzip2 to do that.

For some information, My App structure like this below :

//App path --> C:\xampp\htdocs\service

service\
    |
    -- tmp\
    |
    -- app.js
    |
    -- index.html

Here's my code:

var express = require("express");
var fs = require("fs");
var unzip = require("unzip2"); //module for extracting files
var app = express();

var multer = require("multer");
var multer_dest = multer({dest: "./tmp"}).single('zipFile');

app.post("/upload_zip",multer_dest,function(req,res){
    console.log(req.file);      
    fs.createReadStream(req.file.path).pipe(unzip.Extract({path: 'C:\\Test\\extractDest'}));
    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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.