分块编写 brotli 流
Write a brotli stream by chunks
我正在尝试使用订阅频道并通过附加消息将其写入压缩流:
import zlib from 'zlib'
import fs from 'fs'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const stream = brotli.pipe(writeStream)
stream.write('Hello ')
stream.write('world')
stream.close()
不幸的是,文件是一个没有压缩的纯字符串。
我做错了什么?
另一方面,如果我从文件中读取或尝试这样做,它会起作用:
import zlib from 'zlib'
import fs from 'fs'
import {Readable} from 'stream'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
Readable.from('Hello World').pipe(brotli).pipe(writeStream)
但是我失去了写块的能力。
编辑:我成功了,但我不确定为什么:
import zlib from 'zlib'
import fs from 'fs'
import {PassThrough} from 'stream'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const dup = new PassThrough()
const pipedStream = dup.pipe(brotli).pipe(writeStream)
const finish = dup
finish.write('Hello ')
finish.write(' world')
finish.end()
- 为什么我需要直通流而不是直接写入 brotli?
- 为什么我需要写入双工流 (dup) 而不是 pipedStream?
解决方法是:
import zlib from 'zlib'
import fs from 'fs'
const writeStream = fs.createWriteStream('stream.br')
const brotli = zlib.createBrotliCompress()
brotli.pipe(writeStream)
brotli.write('Hello ')
brotli.write('world')
brotli.end()
close() 终止流,不允许底层流使用数据,应该使用 end() 代替
我正在尝试使用订阅频道并通过附加消息将其写入压缩流:
import zlib from 'zlib'
import fs from 'fs'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const stream = brotli.pipe(writeStream)
stream.write('Hello ')
stream.write('world')
stream.close()
不幸的是,文件是一个没有压缩的纯字符串。
我做错了什么?
另一方面,如果我从文件中读取或尝试这样做,它会起作用:
import zlib from 'zlib'
import fs from 'fs'
import {Readable} from 'stream'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
Readable.from('Hello World').pipe(brotli).pipe(writeStream)
但是我失去了写块的能力。
编辑:我成功了,但我不确定为什么:
import zlib from 'zlib'
import fs from 'fs'
import {PassThrough} from 'stream'
const writeStream = fs.createWriteStream('test.br')
const brotli = zlib.createBrotliCompress()
const dup = new PassThrough()
const pipedStream = dup.pipe(brotli).pipe(writeStream)
const finish = dup
finish.write('Hello ')
finish.write(' world')
finish.end()
- 为什么我需要直通流而不是直接写入 brotli?
- 为什么我需要写入双工流 (dup) 而不是 pipedStream?
解决方法是:
import zlib from 'zlib'
import fs from 'fs'
const writeStream = fs.createWriteStream('stream.br')
const brotli = zlib.createBrotliCompress()
brotli.pipe(writeStream)
brotli.write('Hello ')
brotli.write('world')
brotli.end()
close() 终止流,不允许底层流使用数据,应该使用 end() 代替