gpt4 book ai didi

node.js - Stream 写流到读流

转载 作者:行者123 更新时间:2023-12-05 05:26:49 26 4
gpt4 key购买 nike

我在 nodejs 中使用一个可以从读取流中读取的库。我目前正在打开一个文件读取流并将其传递给 lib。但是,我还需要在我正在读取的文件系统上创建文件。所以第 1 步是创建一个写入文件流并将我的数据流式传输到该文件。

  1. 数据写入流----->文件
  2. http响应<-------读取流文件

我不喜欢这个过程的一点是我必须在完成后清理文件系统。有没有一种方法可以直接写入读取流并一步完成所有操作?

编辑:具体示例(使用 https://github.com/ctalkington/node-archiver ):

// some route
function(req, res, next) {
var archive = archiver('zip');
res.attachment("icons.zip");
archive.pipe(res);

// I have to create files on file system
var stream1 = fs.createWriteStream("file1.txt");
var stream2 = fs.createWriteStream("file2.txt");
...
// all stuff is one streaming to file system

// now stream stuff from file system to archive
archive
.append(fs.createReadStream("file1.txt"), { name: 'file1.txt' })
.append(fs.createReadStream("file2.txt"), { name: 'file2.txt' })
.finalize();

}

我不知道如何避免首先流式传输到文件系统。

编辑:

简单的问题真的是这样的:

我可以写入可读流吗?

最佳答案

这似乎是一个简单的答案,真不敢相信我错过了这个。

看起来我只能使用 PassThrough:

http://nodejs.org/api/stream.html#stream_class_stream_passthrough

Class: stream.PassThrough# This is a trivial implementation of a Transform stream that simply passes the input bytes across to the output. Its purpose is mainly for examples and testing, but there are occasionally use cases where it can come in handy as a building block for novel sorts of streams.

关于node.js - Stream 写流到读流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23697080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com