我需要帮助,如何获取zip文件并将其解压缩到内存中,或者将其写入硬盘。
示例网址
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)
})
我不确定asycnc-api是如何工作的,createWriteStream是否等待整个缓冲区?以及如何将其写入磁盘?
//我在谷歌搜索后找到了一个更好的解决方案,How to download and unzip a zip file in memory in NodeJs?
答案 0 :(得分:1)
# 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' }));
或者如果您不需要zip文件,可以直接从内存中提取它:
request('http://google.com/doodle.png').pipe(unzip.Extract({ path: 'output/path' }));