Node.js zlib flush() 方法
在压缩流上调用flush() 方法,以便它可以强制zlib返回当前可实现的尽可能多的输出。此输出可能会以损坏的压缩质量为代价返回,但当需要尽早访问数据时,它可能很有用。
句法:
zlib.flush()
参数:此方法不接受任何参数。
返回值:尽可能返回当前数据。
下面的例子说明了在 Node.js 中zlib.flush() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// zlib.flush() method
// Including zlib module
const zlib = require('zlib');
// Constructing createGzip and createGunzip
const input = zlib.createGzip();
const output = zlib.createGunzip();
// Piping
input.pipe(output);
// Write to stream
input.write('GeeksforGeeks');
// Calling flush method
input.flush();
// Check output
output.on('data', (d) => {
console.log('Input: Data flush received '
+ d.length + ' bytes');
});
console.log("Program Complete!");
输出:
Program Complete!
Input: Data flush received 13 bytes
示例 2:
// Node.js program to demonstrate the
// zlib.flush() method
// Including zlib module
const zlib = require('zlib');
// Constructing createGzip and createGunzip
const input = zlib.createGzip();
const output = zlib.createGunzip();
// Piping
input.pipe(output);
// Writing to a stream of data 19000 bytes
input.write('G'.repeat(19000));
// Calling flush method with callback
input.flush(() => {});
// Check output
output.on('data', (d) => {
console.log('Input: Data flush with callback received '
+ d.length + ' bytes');
});
console.log("Program Complete!");
输出:因此,字节大小超过 16384 字节后,您需要回调刷新方法,否则数据将不会被完全刷新。
Program Complete!
Input: Data flush with callback received 16384 bytes
Input: Data flush with callback received 2616 bytes
参考: https://nodejs.org/api/zlib.html#zlib_flushing