简体   繁体   English

解压缩文件无效

[英]Unzip file is not working

Im using the following code from 我使用以下代码

https://github.com/cthackers/adm-zip/wiki/ADM-ZIP-Introduction https://github.com/cthackers/adm-zip/wiki/ADM-ZIP-Introduction

Whant I need is to get a zip file from request(Im using express and I've request and response) and I need to extract(unzip) it to some path(in the example for my local drive) ,where should I put the req and what Im missing here to make it work 我需要的是从请求获取一个zip文件(我使用快递和我的请求和响应)我需要提取(解压缩)它到某个路径(在本地驱动器的示例中),我应该在哪里放req和我在这里失踪的东西让它发挥作用

  fn: function (req, res) {
       var admZip = require('adm-zip');

        var zip = new admZip();

        zip.addLocalFile("C://TestFolder//TestZip");

in the request body im getting the zip file(im using postman and in the body I use the binary and select a zip file) 在请求正文中我获取zip文件(即时通讯使用邮递员,在我体内使用二进制文件并选择一个zip文件)

You could simplify the problem by using form-data instead of binary and using multer . 您可以使用form-data而不是binary和使用multer来简化问题。 You can get the input file by accessing req.file after which you can perform your unzip operation. 您可以通过访问req.file来获取输入文件,之后您可以执行解压缩操作。

For example, you would add to your route: 例如,您将添加到您的路线:

var upload = require('multer')({ dest: 'uploads/' });
var admZip = require('adm-zip');


app.post('/upload-here', upload.single('file'), function (req, res, next) {
    var zip = new admZip(req.file.path);

    zip.extractAllTo("C://TestFolder//TestZip", true);
});

Please try my snippet code : 请尝试我的代码段:

For some information, My App structure like this below : 有关一些信息,我的App结构如下:

my path --> C:\xampp\htdocs\service

service
    |
    -- tmp\
    |
    -- app.js
    |
    -- index.html

Client Side: 客户端:

<html>
<body>
<h3>ZIP Upload:</h3>
<form action="/upload_zip" method="POST" enctype="multipart/form-data">
    Select zip to upload:
    <input type="file" name="zipFile" id="zipFile">
    <input type="submit" value="Upload ZIP" name="submit">
</form>
</body>
</html>

Server Side: 服务器端:

Don't forget using enctype="multipart/form-data" when you post it using postman or something like that... 当你使用邮递员或类似的东西发布它时,不要忘记使用enctype="multipart/form-data" ...

var express = require("express");
var fs = require("fs");
var AdmZip = require('adm-zip');
var app = express();

var multer = require("multer");
var multer_dest = multer({dest: "./tmp"}).single('zipFile');

app.get("/",function(req,res){
    console.log("Show index.html");
    res.sendFile(__dirname+"/"+"index.html");
});

app.post("/upload_zip",multer_dest,function(req,res){
    console.log(req.file);  
    var zip = new AdmZip(req.file.path); 
    zip.extractAllTo("./tmp");
    result = {
        file:req.file,
        message:"File has been extracted"
    };
    fs.unlink(req.file.path, function (e) {
        if (e) throw e;
        console.log('successfully deleted '+req.file.path);
    });
    res.end(JSON.stringify(result));
});

var server = app.listen(8081,function(){
    var host = server.address().address;
    var port = server.address().port;

    console.log("Example App Listening at http://%s:%s",host,port);
})

Output : 输出:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM