Nodejs download and unzip file with url, error No header END

I am trying to download files from nseindia.com and unzip them in memory. I am using nodejs webkit and adm-zip. I get an error on the console:

Unused invalid or unsupported zip format. No title END

The code

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

I tried to follow to finish the thread, but failed.

 out.end(); out.destroy(); 

Thanks in advance.

+7
source share
1 answer

You are trying to read the file before it is completely written. You need to wait for the recording to finish.

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

All Articles