将zip文件解压缩到本地文件夹 [英] Extract zip file to local folder

查看:567
本文介绍了将zip文件解压缩到本地文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的节点应用程序有来自客户端的快递和即时发送请求,如邮递员,我需要从 req 获取文件并将其解压缩到我的本地文件夹,我该怎么做?



我发现以下开源但不知道如何使用 req body
并将其解压缩到我的本地文件夹中,如C: //测试// extractDest,



以下代码取自以下开源,但如果是其他良好的开源,我可以使用它相反

解决方案

我建议你使用


I have node app with express and im sending request from client like postman and I need to get the file from the req and extract it on my local folder,how I can do that ?

I found the following open source but not sure how to take the req body and extract it in my local folder like "C://Test//extractDest",

The code below is taken from the following open source but if there is other good open source for this I can use it instead https://www.npmjs.com/package/decompress-zip

var DecompressZip = require('decompress-zip');
var unzipper = new DecompressZip(filename)

unzipper.on('error', function (err) {
    console.log('Caught an error');
});

unzipper.on('extract', function (log) {
    console.log('Finished extracting');
});

unzipper.on('progress', function (fileIndex, fileCount) {
    console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
});

unzipper.extract({
    path: "C://Test//extractDest",
    filter: function (file) {
        return file.type !== "SymbolicLink";
    }
});

This is how I send the zip file I simply select binary and choose a zip file

解决方案

I'd recommend you using multer which works with multipart/form-data content-type.

Here's a basic working example. It assumes we are only uploading a single file, a folder named "uploads" exists at the root of your project and a form field named "singleFileUpload" to hold your data. You can change all those following the multer documentation:

var path     = require("path");
var express  = require("express");
var multer   = require("multer");
var Unzipper = require("decompress-zip");


var app = express();

app.use(multer({dest:'./uploads/'}).single('singleFileUpload'));


app.post("/", function(req, res){

    if (req.file){

        var filepath = path.join(req.file.destination, req.file.filename);
        var unzipper = new Unzipper(filepath);

        unzipper.on("extract", function () {
            console.log("Finished extracting");
        });

        unzipper.extract({ path: "C://Test//extractDest"});
    }

    res.status(204).end();
});


app.listen(3000);

Using postman you can now upload and decompress files with this configuration:

这篇关于将zip文件解压缩到本地文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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