2

I need help, how to fetch a zip file and unzip it to memory, or maybe write it to harddisk.

sample url

var http = require("http");
var fs = require("fs");
var url = "http://www.caltrain.com/Assets/GTFS/caltrain/Caltrain-GTFS.zip"
var file = fs.createWriteStream("./file.zip");

http.get(url, (res) => {
    var test = res.pipe(file);
    console.log("it's finished")
    console.log(test)
})

I am not sure how the asycnc-api works, does createWriteStream wait for the whole buffer? and how to write it to disk?

// I find a better solution after hours of googling, How to download and unzip a zip file in memory in NodeJs?

Community
  • 1
  • 1
vdj4y
  • 2,649
  • 2
  • 21
  • 31
  • Please show your work. What have you tried so far? – Soviut Sep 26 '16 at 20:23
  • Use for example [request](https://github.com/request/request) and [unzip](https://github.com/EvanOxfeld/node-unzip). Also useful to read about [fs](https://nodejs.org/api/fs.html) and [streams](https://nodejs.org/api/stream.html). – Frxstrem Sep 26 '16 at 20:28
  • hi, i am confused as when the file has finished downloading, I keep getting an "write after end" error – vdj4y Sep 26 '16 at 20:29
  • are you trying to unzip it to ram or disk? you mentioned both – danilopopeye Sep 26 '16 at 20:42
  • hi danilopopeye, either way is fine. The easier one to perform – vdj4y Sep 26 '16 at 20:45
  • well, you can tag it as duplicate, I am not that familiar with stack overflow – vdj4y Sep 27 '16 at 20:51

1 Answers1

3

You can use the mentioned packages: request & unzip

# download and save the file to filesystem
request('http://google.com/doodle.png').pipe(fs.createWriteStream('file.zip'))

# extract it to `output/path`
fs.createReadStream('file.zip').pipe(unzip.Extract({ path: 'output/path' }));

Or if you don't need the zip file, you can extract it directly from memory:

request('http://google.com/doodle.png').pipe(unzip.Extract({ path: 'output/path' }));
danilopopeye
  • 9,610
  • 1
  • 23
  • 31
  • I am also trying to understand nodejs. The code uses async module but written as sync, I am having trouble with understanding the timing. – vdj4y Sep 26 '16 at 20:55
  • not sure what you mean by "the code uses async module but written as sync". my snippet didn't work? – danilopopeye Sep 26 '16 at 21:04
  • I try your code, unzip.Extract({ path: 'output/path' } create new folder , but folder is empty. I am assuming, it is because fs is still empty... does .pipe() accept callback ?? – vdj4y Sep 26 '16 at 21:10
  • the second alternative code works, but the first code does not work. anyway thanks – vdj4y Sep 26 '16 at 21:36