如何在Node.js中一次将一个可读流传输到两个可写流中?

时间:2013-01-05 15:55:28

标签: node.js gzip zlib gzipstream node.js-stream

目标是:

  1. 创建文件读取流。
  2. 将其传输到gzip(zlib.createGzip()
  3. 然后将zlib输出的读取流传输到:

    1)HTTP response对象

    2)可写文件流以保存gzip压缩输出。

  4. 现在我可以做到3.1:

    var gzip = zlib.createGzip(),
        sourceFileStream = fs.createReadStream(sourceFilePath),
        targetFileStream = fs.createWriteStream(targetFilePath);
    
    response.setHeader('Content-Encoding', 'gzip');
    
    sourceFileStream.pipe(gzip).pipe(response);
    

    ...工作正常,但我还需要将gzip压缩数据保存到文件,这样我就不需要每次都重新压缩,并且能够直接将压缩数据流式传输为回应。

    那么如何在Node中一次将一个可读流传输到两个可写流中?

    sourceFileStream.pipe(gzip).pipe(response).pipe(targetFileStream);会在Node 0.8.x中运行吗?

3 个答案:

答案 0 :(得分:46)

管道链接/拆分不像你在这里尝试那样工作,发送第一个到两个不同的后续步骤:

sourceFileStream.pipe(gzip).pipe(response);

但是,您可以将相同的可读流传输到两个可写流中,例如:

var fs = require('fs');

var source = fs.createReadStream('source.txt');
var dest1 = fs.createWriteStream('dest1.txt');
var dest2 = fs.createWriteStream('dest2.txt');

source.pipe(dest1);
source.pipe(dest2);

答案 1 :(得分:11)

我发现zlib返回一个可读的流,稍后可以将其传输到多个其他流中。所以我做了以下工作来解决上述问题:

var sourceFileStream = fs.createReadStream(sourceFile);
// Even though we could chain like
// sourceFileStream.pipe(zlib.createGzip()).pipe(response);
// we need a stream with a gzipped data to pipe to two
// other streams.
var gzip = sourceFileStream.pipe(zlib.createGzip());

// This will pipe the gzipped data to response object
// and automatically close the response object.
gzip.pipe(response);

// Then I can pipe the gzipped data to a file.
gzip.pipe(fs.createWriteStream(targetFilePath));

答案 2 :(得分:0)

您可以使用“可读流克隆”包

const fs = require("fs");
const ReadableStreamClone = require("readable-stream-clone");

const readStream = fs.createReadStream('text.txt');

const readStream1 = new ReadableStreamClone(readStream);
const readStream2 = new ReadableStreamClone(readStream);

const writeStream1 = fs.createWriteStream('sample1.txt');
const writeStream2 = fs.createWriteStream('sample2.txt');

readStream1.pipe(writeStream1)
readStream2.pipe(writeStream2)