我尝试找到可以发送zip的示例(例如通过邮递员)
并在我的处理程序中获取此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 and multer 在我看来,会很棒
更新-这是邮递员请求
答案 0 :(得分:2)
没有完整的示例,很难说出真正的问题是什么。但是根据Express docs,它说:
在Express 4中,req对象不再在req对象上可用 默认。要访问req.files对象上的上传文件,请使用 多部分处理中间件,例如busboy,multer,强大, 多方,连接多方或pez。
因此,如果您不使用中间件库来处理文件上传,则很难确定req.file
的值是什么。
我还有些担心您尝试使用zlib
解压缩zip文件,因为library仅支持gzip。
zlib模块提供了使用以下命令实现的压缩功能 Gzip和Deflate / Inflate
您需要检查req.file.mimetype === 'application/gzip'
以下是一些与解压缩zip文件有关的帖子:
答案 1 :(得分:2)
首先,zlib
不支持提取zip
文件。
我建议使用formidable
处理文件,因为
先决条件
使用npm i -S extract-zip formidable express
或yarn add extract-zip formidable express
formidable
和extract-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
目录中。
答案 2 :(得分:2)
先决条件:
npm i express unzipper multiparty bluebird
app/web
目录(或根据需要自动创建)。async/await
的节点版本(据我所知,为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,
};
用法:
node server
启动服务器file
字段中发送文件(邮递员的密钥file
)。 curl curl -XPOST -F 'file=@../ttrra-dsp-agency-api/banner.zip' 'localhost:3000/files'
)中的示例缺点:
答案 3 :(得分:-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;