I want to upload a zip file into the server using node.So can any one help me to figure it out.
2 Answers
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
4 Comments
Saichandhra Arvapally
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
Samim Hakimi
Please check with unzipper module.
Andrew Howard
@Samim do you always have to temporarily put the contents into a temporary folder before unzipping?
Andrew Howard
Works great for me. Just one minor correction: const unzipper = require('./unzip'); should be: const unzipper = require('unzipper');