简体   繁体   English

节点从 URL 中读取大型 zip 文件,然后解压缩

[英]Node Read large zip file from URL and then Unzip it

I am reading a large zip file(500MB) from a URL using request.get(url).File contains one CSV file which is pretty huge in size.我正在使用 request.get(url) 从 URL 中读取一个大的 zip 文件(500MB)。文件包含一个非常大的 CSV 文件。 I am reading the response and writing the filestream into a zip file using fs.createWriteStream(zipFile).我正在使用 fs.createWriteStream(zipFile) 读取响应并将文件流写入 zip 文件。 On close event of the fs.createWriteStream I have tried using adm-zip file with which i got "error invalid or unsupported zip format. no end header found" and with Unzipper npm package I am getting "invalid signature unzip ". On close event of the fs.createWriteStream I have tried using adm-zip file with which i got "error invalid or unsupported zip format. no end header found" and with Unzipper npm package I am getting "invalid signature unzip ". Below is the code下面是代码

    const request = require('superagent');
const fs = require('fs');
const unzip = require('unzipper');
request.get(url).on('error', function(err) { console.log(err) }
                .pipe(fs.createWriteStream(zipFile))
                .on('close', function() {
                    const admZip = require('adm-zip');
                    console.log('start unzip');
                    var zip = new admZip(zipFile);
                    console.log('unzipping ' + uploadDir + "to");
                    zip.extractAllTo(uploadDir, true);
                    console.log('finished unzip');

with Unzipper带解压器

 request.get(url)
            .on('error', function(err) { console.log(err) }
                .pipe(fs.createWriteStream(zipFile))
                .on('close', function() {
                    fs.createReadStream(zipFile)
                        .pipe(unzipper.Extract({ path: UploadDir }));`enter code here`
                })

Error is resolved.错误已解决。 First step is to pipe the incoming readable response stream.第一步是 pipe 传入可读响应 stream。

request.get(urlData)
  .pipe(writeStream);

Once the read is done pipe will trigger the write stream.一旦读取完成 pipe 将触发写入 stream。 Then I am triggering the unzipping process on close event of the writestream.然后我在 writestream 的关闭事件上触发解压缩过程。

writeStream.on('close', function() {      
  fs.createReadStream(zipFile).pipepipe(unzip.Extract({
    path: uploadDir
  }));
  console.log('finished unzip');
});

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

相关问题 节点https从url获取文件并解压缩 - Node https get file from url and unzip 有没有办法从 url 请求中解压缩文件并在节点中解析其内容? - Is there a way to unzip a file from a url request and parse its content in node? 如何从终端解压缩 zip 文件? - How to unzip a zip file from the Terminal? Node.js - 压缩/解压缩文件夹 - Node.js - Zip/Unzip a folder Node WebKit应用程序中的docompress-zip无法解压缩下载的文件,根据WinRAR,下载的文件“损坏” - docompress-zip in Node WebKit app cannot unzip downloaded file, downloaded file “corrupt” according to WinRAR 将文件解压缩到内存中,调整文件,压缩并流式传输到客户端(Node.js) - Unzip file into memory, tweak file, zip and stream to the client (Node.js) 如何在 node-js 中读取和处理大型 zip 文件 - how to read and process large zip files in node-js 在完成 nodeJS 后从 Google Cloud Functions 中的电子邮件中上传和解压缩 .zip 文件删除 .zip - Upload and unzip .zip file from E-Mail in Google Cloud Functions deleting .zip after completion nodeJS 如何将 Node.js 中的 .zip/.rar 文件解压缩到文件夹中 - How do I unzip a .zip/.rar file in Node.js into a folder 从node.js发送的jszip解压缩文件zip损坏:缺少37个字节 - jszip unzip flie sent from node.js Corrupted zip: missing 37 bytes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM