Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload a zip file using nodejs and extract it?

Tags:

node.js

zip

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

like image 849
Saichandhra Arvapally Avatar asked Apr 27 '20 11:04

Saichandhra Arvapally


People also ask

How do I extract files from a zip file?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

How do I read a zip file in node JS?

Inside the loop, you convert the object into a string that represents the object using the Node. js toString() method, then log it in the console using the console. log() method. Finally, you invoke the readZipArchive() function with the ZIP archive file path as an argument.

Can you upload a zip file?

You can upload a zip file (or any other type of file) following the normal procedure for uploading a file to Google Drive.


1 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
like image 162
Samim Hakimi Avatar answered Nov 15 '22 05:11

Samim Hakimi