【问题标题】:node.js, fetch a zip file and unzip it to ram [duplicate]node.js,获取一个zip文件并将其解压缩到ram [重复]
【发布时间】:2016-09-26 20:21:28
【问题描述】:

我需要帮助,如何获取 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?

【问题讨论】:

  • 请展示你的作品。到目前为止,您尝试过什么?
  • 例如使用requestunzip。阅读fsstreams 也很有用。
  • 嗨,我很困惑,因为文件下载完成后,我一直收到“结束后写入”错误
  • 您是要解压到内存还是磁盘?你提到了两个
  • 嗨 danilopopeye,无论哪种方式都可以。更容易执行

标签: node.js node.js-stream


【解决方案1】:

您可以使用上述软件包: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' }));

或者如果你不需要zip文件,你可以直接从内存中解压:

request('http://google.com/doodle.png').pipe(unzip.Extract({ path: 'output/path' }));

【讨论】:

  • 我也在尝试理解nodejs。该代码使用异步模块,但编写为同步,我无法理解时间。
  • 不确定“代码使用异步模块但编写为同步”是什么意思。我的 sn-p 没用?
  • 我试试你的代码, unzip.Extract({ path: 'output/path' } create new folder ,但文件夹是空的。我假设,这是因为 fs 仍然是空的...... .pipe() 接受回调??
  • 第二个替代代码有效,但第一个代码无效。无论如何谢谢
猜你喜欢
  • 2014-10-03
  • 1970-01-01
  • 2021-04-29
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2016-10-13
  • 2019-05-01
  • 2020-05-02
相关资源
最近更新 更多