作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试从 nseindia.com 下载文件并在内存中解压缩。我正在使用 nodejs webkit 和 adm-zip。我在控制台上收到错误消息:
Uncaught Invalid or unsupported zip format. No END header found
代码:
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);
});
关于node.js - nodejs从url下载并解压文件,错误No END header found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14107470/
我是一名优秀的程序员,十分优秀!