13

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

Uncaught Invalid or unsupported zip format. No END header found

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 following to end the stream but no success.

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

Thanks in advance.

Muath
  • 4,351
  • 12
  • 42
  • 69
mrkanitkar
  • 169
  • 1
  • 1
  • 6

1 Answers1

10

You are trying to read file before it completely written. You need to wait for finish writing.

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);
});
Vadim Baryshev
  • 25,689
  • 4
  • 56
  • 48
  • Thanks Vadim. It worked. One question, if downloaded file (which we are unzipping)have multiple zips in it, how do we extract them as well? There is no "end" event to adm-zip. – mrkanitkar Jan 01 '13 at 16:27
  • As far as i see method `extractAllTo` is synchronous. It blocks event loop until extraction is complete. So you don't need `end` event here. Be careful with synchronous libs. It`s not node.js way and may cause freezes on highload. – Vadim Baryshev Jan 01 '13 at 20:14
  • 1
    `req.on('end', ...` is now `req.on('finish', ...` – uri gat Jan 13 '19 at 17:00
  • It gives me same error if I point to file url. Any solution ? Following is the code: `const zip = new AdmZip("C://folder//test.zip");` – Mike143 Sep 05 '19 at 19:40