Node.js nodejs从url下载并解压缩文件,错误:未找到结束头
我正在尝试从nseindia.com下载文件并在内存中解压缩。我正在使用nodejswebkit和adm-zip。我在控制台上遇到错误: 未捕获无效或不支持的zip格式。未找到结束标头 代码:Node.js nodejs从url下载并解压缩文件,错误:未找到结束头,node.js,download,unzip,adm-zip,Node.js,Download,Unzip,Adm Zip,我正在尝试从nseindia.com下载文件并在内存中解压缩。我正在使用nodejswebkit和adm-zip。我在控制台上遇到错误: 未捕获无效或不支持的zip格式。未找到结束标头 代码: var http = require('http'), fs = require('fs'), request = require('request'), AdmZip = require('adm-zip')
var http = require('http'),
fs = require('fs'),
request = require('request'),
AdmZip = require('adm-zip'),
out = fs.createWriteStream('data/nseeqbhav.zip'); // For saving NSE Equity bhavcopy
// Downloading NSE Bhavcopy
request(
{ method: 'GET',
uri: 'http://www.nseindia.com/content/historical/EQUITIES/2012/DEC/cm19DEC2012bhav.csv.zip',
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Referer": "http://www.nseindia.com/products/content/all_daily_reports.htm",
"Accept-Encoding": "gzip,deflate,sdch",
"encoding": "null",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
).pipe(out);
var zip = new AdmZip("data/nseeqbhav.zip"),
zipEntries = zip.getEntries();
zip.extractAllTo(/*target path*/"data/unzip/", /*overwrite*/true);
我试着跟着结束这条小溪,但没有成功
out.end();
out.destroy();
提前感谢。您正在尝试在文件完全写入之前读取该文件。你需要等待写完
var http = require('http'),
fs = require('fs'),
request = require('request'),
AdmZip = require('adm-zip'),
out = fs.createWriteStream('data/nseeqbhav.zip'); // For saving NSE Equity bhavcopy
// Downloading NSE Bhavcopy
var req = request(
{
method: 'GET',
uri: 'http://www.nseindia.com/content/historical/EQUITIES/2012/DEC/cm19DEC2012bhav.csv.zip',
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Referer": "http://www.nseindia.com/products/content/all_daily_reports.htm",
"Accept-Encoding": "gzip,deflate,sdch",
"encoding": "null",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
);
req.pipe(out);
req.on('end', function() {
var zip = new AdmZip("data/nseeqbhav.zip"),
zipEntries = zip.getEntries();
zip.extractAllTo(/*target path*/"data/unzip/", /*overwrite*/true);
});
谢谢瓦迪姆。成功了。一个问题是,如果下载的文件(我们正在解压)中有多个拉链,我们如何提取它们呢?adm-zip没有“结束”事件。据我所知,方法
extractAllTo
是同步的。它会阻止事件循环,直到提取完成。因此,这里不需要end
事件。要小心使用同步lib。它不是node.js方式,可能会导致高负载时冻结。req.on('end'),…
现在是req.on('finish'),…
如果我指向文件url,它会给我同样的错误。有解决方案吗?下面是代码:const-zip=new-AdmZip(“C://folder//test.zip”);