如何使用 nodejs 上传 zip 文件并解压缩?

Sai*_*lly 4 zip node.js

我想使用 node.So 将一个 zip 文件上传到服务器。所以有人可以帮我弄清楚。

Sam*_*imi 5

首先使用 Multer 上传您的 zip 文件:

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 })
Run Code Online (Sandbox Code Playgroud)

然后使用解压缩模块解压缩它:

1) 安装解压模块

npm i unzipper
Run Code Online (Sandbox Code Playgroud)

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();
    }
  });
Run Code Online (Sandbox Code Playgroud)

//来源

测试:

c:\Samim>node ExtractZip.js
Run Code Online (Sandbox Code Playgroud)