6

I want to upload a zip file into the server using node.So can any one help me to figure it out.

2 Answers 2

7

First upload your zip file using Multer:

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

var upload = multer({ storage: storage })

Then unzip it using unzipper module:

1) Install unzipper module

npm i unzipper

2) ExtractZip.js JavaScript

const unzipper = require('./unzip');
var fs = require('fs');


fs.createReadStream('path/to/archive.zip')
  .pipe(unzipper.Parse())
  .on('entry', function (entry) {
    const fileName = entry.path;
    const type = entry.type; // 'Directory' or 'File'
    const size = entry.vars.uncompressedSize; // There is also compressedSize;
    if (fileName === "this IS the file I'm looking for") {
      entry.pipe(fs.createWriteStream('output/path'));
    } else {
      entry.autodrain();
    }
  });

// Source

Test:

c:\Samim>node ExtractZip.js
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you samim for your response. But when i'm using fs ,i'm getting this error ReferenceError: primordials is not defined [SERVER] at fs.js:35:5
Please check with unzipper module.
@Samim do you always have to temporarily put the contents into a temporary folder before unzipping?
Works great for me. Just one minor correction: const unzipper = require('./unzip'); should be: const unzipper = require('unzipper');
0

You can try multer npm install --save multer

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, '/tmp/my-uploads')
  },
  filename: function (req, file, cb) {
    cb(null, file.fieldname + '-' + Date.now())
  }
})

var upload = multer({ storage: storage })

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.