在壓縮流上調用 flush() 方法,以便它可以強製 zlib 返回當前可實現的盡可能多的輸出。此輸出可能會以損壞壓縮質量為代價返回,但當需要盡早訪問數據時,它可能很有用。
用法:
zlib.flush()
參數:該方法不接受任何參數。
返回值:它返回當前盡可能多的數據。
下麵的例子說明了 zlib.flush() 方法在 Node.js 中的使用:
示例 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字節後,您需要回調flush方法,否則數據將不會被完全刷新。
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
相關用法
- Node.js zlib.createBrotliCompress()用法及代碼示例
 - Node.js zlib.inflateRaw()用法及代碼示例
 - Node.js zlib.gunzip()用法及代碼示例
 - Node.js zlib.gzip()用法及代碼示例
 - Node.js zlib.inflate()用法及代碼示例
 - Node.js zlib.createBrotliDecompress()用法及代碼示例
 - Node.js zlib.deflateRaw()用法及代碼示例
 - Node.js zlib.createUnzip()用法及代碼示例
 - Node.js zlib.createDeflateRaw()用法及代碼示例
 - Node.js zlib.createGunzip()用法及代碼示例
 - Node.js zlib.createInflateRaw()用法及代碼示例
 - Node.js zlib.createGzip()用法及代碼示例
 - Node.js zlib.unzip()用法及代碼示例
 - Node.js zlib.createDeflate()用法及代碼示例
 - Node.js zlib.createInflate()用法及代碼示例
 - Node.js zlib.brotliDecompress()用法及代碼示例
 - Node.js zlib.brotliCompress()用法及代碼示例
 - Node.js zlib.inflateSync()用法及代碼示例
 - Node.js zlib.inflateRawSync()用法及代碼示例
 - Node.js zlib.deflateRawSync()用法及代碼示例
 - Node.js zlib.gunzipSync()用法及代碼示例
 - Node.js zlib.deflateSync()用法及代碼示例
 - Node.js zlib.brotliDecompressSync()用法及代碼示例
 - Node.js zlib.deflate()用法及代碼示例
 - Node.js zlib.unzipSync()用法及代碼示例
 
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js zlib.flush() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
