将 zip 文件通过 express 解压到指定文件夹节点

12

我试图找到一个例子,在其中可以发送一个zip文件(例如通过postman),并在我的处理程序中获取这个zip文件,解压缩 它到指定的文件夹。 我没有找到很多使用express进行压缩的示例, 我希望将其解压缩到路径web/app

我尝试了以下代码,但对我无效,压缩文件未解压缩到指定的文件夹中,你有什么想法吗?

https://nodejs.org/api/zlib.html#zlib_zlib

var zlib = require('zlib');
var fs = require('fs');
const dir = path.join(__dirname, 'web/app/');

if (req.file.mimetype === 'application/zip') {

    var unzip = zlib.createUnzip();

    var read = fs.createReadStream(req.file);
    var write = fs.createWriteStream(dir);
    //Transform stream which is unzipping the zipped file
    read.pipe(unzip).pipe(write);   
    console.log("unZipped Successfully");

}

任何工作示例将非常有帮助,或者提供我可能遇到问题的参考资料...

调试时,我看到代码失败的原因是:

var read = fs.createReadStream(req.file);

有任何想法吗?

我还尝试过:

var read = fs.createReadStream(req.file.body);

问题是我没有看到错误、原因等。

当我将它更改为:

var read = fs.createReadStream(req.file.buffer);

程序不退出,我能够运行它直到记录器 console.log("unZipped Successfully"); 但是没有任何反应...

如果在我的上下文中有使用yauzl 和 multerhttps://www.npmjs.com/package/yauzl 示例,那将非常棒。

更新- 这是Postman请求:

enter image description here


接收文件总是有点麻烦。也许你可以尝试保存文件并使用GUI打开进行调试?你可能会得到有价值的信息。一步一步来。 - Lars C. Magnusson
这很容易使用操作系统脚本完成。您没有指定任何限制,有吗? - Emeeus
4个回答

8

首先,zlib 不支持提取 zip 文件。

我建议使用formidable来处理文件,因为:

  1. 它已经经受过实战考验
  2. 被广泛使用
  3. 避免了写读取请求中的文件流、存储和处理错误等样板代码
  4. 易于配置

前提条件
使用 npm i -S extract-zip formidable expressyarn add extract-zip formidable express 安装依赖项。

使用 formidableextract-zip 的最简解决方案:

const express = require('express');
const fs = require('fs');
const extract = require('extract-zip')
const formidable = require('formidable');
const path = require('path');
const uploadDir = path.join(__dirname, '/uploads/');
const extractDir = path.join(__dirname, '/app/');
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir);
}
if (!fs.existsSync(extractDir)) {
  fs.mkdirSync(extractDir);
}

const server = express();

const uploadMedia = (req, res, next) => {
  const form = new formidable.IncomingForm();
  // file size limit 100MB. change according to your needs
  form.maxFileSize = 100 * 1024 * 1024;
  form.keepExtensions = true;
  form.multiples = true;
  form.uploadDir = uploadDir;

  // collect all form files and fileds and pass to its callback
  form.parse(req, (err, fields, files) => {
    // when form parsing fails throw error
    if (err) return res.status(500).json({ error: err });

    if (Object.keys(files).length === 0) return res.status(400).json({ message: "no files uploaded" });
    
    // Iterate all uploaded files and get their path, extension, final extraction path
    const filesInfo = Object.keys(files).map((key) => {
      const file = files[key];
      const filePath = file.path;
      const fileExt = path.extname(file.name);
      const fileName = path.basename(file.name, fileExt);
      const destDir = path.join(extractDir, fileName);

      return { filePath, fileExt, destDir };
    });

    // Check whether uploaded files are zip files
    const validFiles = filesInfo.every(({ fileExt }) => fileExt === '.zip');

    // if uploaded files are not zip files, return error
    if (!validFiles) return res.status(400).json({ message: "unsupported file type" });

    res.status(200).json({ uploaded: true });

    // iterate through each file path and extract them
    filesInfo.forEach(({filePath, destDir}) => {
      // create directory with timestamp to prevent overwrite same directory names
      extract(filePath, { dir: `${destDir}_${new Date().getTime()}` }, (err) => {
        if (err) console.error('extraction failed.');
      });
    });
  });

  // runs when new file detected in upload stream
  form.on('fileBegin', function (name, file) {
    // get the file base name `index.css.zip` => `index.html`
    const fileName = path.basename(file.name, path.extname(file.name));
    const fileExt = path.extname(file.name);
    // create files with timestamp to prevent overwrite same file names
    file.path = path.join(uploadDir, `${fileName}_${new Date().getTime()}${fileExt}`);
  });
}

server.post('/upload', uploadMedia);

server.listen(3000, (err) => {
  if (err) throw err;
});

这个解决方案适用于单个/多个文件上传。这个解决方案唯一的问题是,即使服务器抛出错误,也会上传错误的文件类型到uploaded目录。

使用Postman进行测试: postman image


1
非常感谢,问题已解决! - user4445419
非常好的答案,对我有所帮助! - Javier

2
没有完整的示例,很难确定真正的问题所在。但根据Express文档所述:

在Express 4中,默认情况下不再在req对象上提供req.files。要访问req.files对象上上传的文件,请使用multipart-handling中间件,如busboy、multer、formidable、multiparty、connect-multiparty或pez。

因此,如果您没有使用中间件库来处理上传文件,则很难确定req.file的值是什么。
我还有点担心您尝试使用zlib解压缩zip文件,因为该仅支持gzip格式。

zlib模块提供了使用Gzip和Deflate/Inflate实现的压缩功能

您需要检查req.file.mimetype === 'application/gzip'
以下是一些与解压缩zip文件相关的帖子:

req.file.mimetype 基于文件扩展名工作,而不是基于文件类型。 - muthukumar selvaraj

2

前提条件:

  1. npm i express unzipper multiparty bluebird
  2. 在项目根目录中创建 app/web 目录(如果需要,您可以自动创建)。
  3. 将所有这些文件放入一个目录中。
  4. 支持 async/await 的 Node 版本(据我所知,至少是 7.6+)

server.js:

const express = require('express');
const Promise = require('bluebird');
const fs = require('fs');
const writeFile = Promise.promisify(fs.writeFile);

const { parseRequest, getFile } = require('./multipart');
const { extractFiles } = require('./zip')

const EXTRACT_DIR = 'web/app';

const app = express();

const uploadFile = async (req, res, next) => {
  try {
    const body = await parseRequest(req);
    const bodyFile = getFile(body, 'file');
    if (!/\.zip$/.test(bodyFile.originalFilename)) {
      res.status(200).json({ notice: 'not a zip archive, skipping' })
      return;
    }
    const archiveFiles = await extractFiles(bodyFile);

    await Promise.each(archiveFiles, async (file) => {
      await writeFile(EXTRACT_DIR + '/' + file.path, file.buffer);
    })
    res.status(200).end();
  } catch (e) {
    res.status(500).end();
  }
};

app.post('/files', uploadFile);

app.listen(3000, () => {
  console.log('App is listening on port 3000');
});

multipart.js

const Promise = require('bluebird');
const { Form } = require('multiparty');

function parseRequest (req, options) {
    return new Promise((resolve, reject) => {
        const form = new Form(options)
        form.parse(req, (err, fields, files) => {
            if (err) {
                return reject(err);
            }
            return resolve({ fields, files });
        });
    });
}

function getFile (body, field) {
    const bodyFile = body.files[field];
    const value = bodyFile ? bodyFile[0] : null;
    return value || null;
}

module.exports = {
    parseRequest,
    getFile,
};

zip.js

const unzip = require('unzipper');
const fs = require('fs');

async function extractFiles (file) {
    const files = [];
    await fs.createReadStream(file.path).pipe(unzip.Parse()).on('entry', async entry => {
    // Cleanup system hidden files (or drop this code if not needed)
        if (
            entry.type !== 'File'
            || /^__MACOSX/.test(entry.path)
            || /.DS_Store/.test(entry.path)
        ) {
            entry.autodrain()
            return
        }
        const pathArr = entry.path.split('/');
        const path = entry.path;
        const buffer = await entry.buffer();
        files.push({ buffer, path, originalFilename: pathArr[pathArr.length - 1] });
    }).promise();
    return files;
}

module.exports = {
    extractFiles,
};

使用方法:

  1. 使用node server启动服务器。
  2. 在请求的file字段中发送您的文件 (在postman中键为file)。示例curl命令:curl -XPOST -F 'file=@../ttrra-dsp-agency-api/banner.zip' 'localhost:3000/files'

缺点:

  1. 解压后的文件存储在缓冲区中,因此此方法不适用于大型档案,不建议使用

谢谢,我想检查它,但是我不能使用unzipper,因为它不支持大文件,只能使用yauzl和上面的包装器extract-zip,谢谢。 - user4445419
另外,如果您能添加创建应用程序文件夹的代码(如果不存在),那将非常好。 - user4445419

-1
这是我上传文件到express服务器的代码。
//require express library
var express = require('express');
//require the express router
var router = express.Router();
//require multer for the file uploads
var multer = require('multer');

//File Upload

var storage = multer.diskStorage({
  // destino del fichero
  destination: function (req, file, cb) {
    cb(null, './uploads/logo')
  },
  // renombrar fichero
  filename: function (req, file, cb) {
    cb(null, file.originalname);
  }
});

var upload = multer({ storage: storage });

router.post("/", upload.array("uploads[]", 1), function (req, res) {
  res.json('Uploaded logo successfully');
});


module.exports = router; 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接