gpt4 book ai didi

javascript - 通过 express 将 zip 解压缩到文件夹 Node 中

转载 作者:IT老高 更新时间:2023-10-28 23:26:33 38 4
gpt4 key购买 nike

我尝试找到可以发送 zip 的示例(例如通过 postman )并在我的处理程序中获取此 zip 并 解压缩 它如此指定的文件夹我没有找到太多使用 express 进行压缩的示例我想解压到路径 web/app

我尝试了以下对我不起作用的方法,zip 文件没有解压缩到指定的文件夹中,知道我做错了什么吗?

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"); 但什么也没发生...

如果有 https://www.npmjs.com/package/yauzl 的任何示例yauzl 和 multer 在我的上下文中,它会很棒

更新-这是 postman 的请求

enter image description here

最佳答案

首先,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

关于javascript - 通过 express 将 zip 解压缩到文件夹 Node 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244720/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com