[英]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.