vdj4y vdj4y - 26 days ago 7x
Node.js Question

node.js, fetch a zip file and unzip it to ram

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

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?


You can use the mentioned packages: request & unzip

# download and save the file to filesystem

# 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' }));