2013-01-01 63 views
7

我試圖從nseindia.com下載文件並在內存中解壓。我正在使用nodejs webkit和adm-zip。我在控制檯上出現錯誤:nodejs從url下載並解壓縮文件,發現錯誤無END標頭

未捕獲無效或不支持的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(); 

在此先感謝。

回答

8

您正在嘗試在完全寫入之前讀取文件。你需要等待寫完。

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); 
}); 
+0

謝謝Vadim。有效。一個問題,如果下載的文件(我們正在解壓)有多個拉鍊,我們如何提取它們呢? adm-zip沒有「結束」事件。 – mrkanitkar

+0

據我所知,方法'extractAllTo'是同步的。它會阻止事件循環,直到提取完成。所以你不需要在這裏結束事件。請注意同步庫。它不是node.js的方式,可能會導致高負載凍結。 –