11

我正在尝试从 nseindia.com 下载文件并在内存中解压缩。我正在使用 nodejs webkit 和 adm-zip。我在控制台上收到错误:

未捕获 无效或不受支持的 zip 格式。未找到 END 标头

代码

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(); 

提前致谢。

4

1 回答 1

9

您正在尝试在文件完全写入之前读取文件。您需要等待完成写入。

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);
});
于 2013-01-01T04:25:59.107 回答