还是要使用Multer解压缩上传到内存中的文件吗? [英] Anyway to unzip a file uploaded in memory using Multer?

查看:33
本文介绍了还是要使用Multer解压缩上传到内存中的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Multer来获取在表单上上传的文件并将其保存在内存中,以便我可以解析它并吐出一些东西,但是有一种方法可以获取zip文件并能够将其解压缩仍将未压缩的文件保留在内存中而不写入磁盘?我知道节点有一个名为'unzip'的npm模块,但是它似乎只能从物理位置提取文件,除非我弄错了.我感谢任何见识!

I am using Multer to take a file uploaded on a form and keep it in memory so that I can parse it and spit out some stuff, but is there a way I can take in a zip file and be able to unzip it and still keep the unzipped files in memory without writing to a disk? I know there is an npm module for node called 'unzip', but it seems to only be able to pull a file from a physical location, unless I am mistaken. I appreciate any insight!

const multer  = require('multer');
const storage = multer.memoryStorage();
const upload = multer({ storage: storage, putSingleFilesInArray: true });

// landing page
router.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, '../../views', 'index.html'));
});

// parse multiple files from the form
router.post('/upload', upload.array('upload'), function (req, res) {
    console.log(req.files);
});

推荐答案

使用AdmZip npm软件包(如下所示)已解决.

Solved using AdmZip npm package (shown below).

var AdmZip = require('adm-zip');

if (req.files[0].mimetype == 'application/x-zip-compressed'){
    var zip = new AdmZip(req.files[0].buffer);
    var zipEntries = zip.getEntries();
    zipEntries.forEach(function(zipEntry) {
         //do stuff
    });

这篇关于还是要使用Multer解压缩上传到内存中的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆