Node.js zlib.close() 方法
zlib.close() 方法是 Zlib 模块的内置应用程序编程接口,用于关闭底层句柄。
句法:
zlib.close( callback )
参数:此方法接受包含回调函数的单个参数回调。
下面的例子说明了 Node.js 中zlib.close() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// zlib.close() 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();
// Calling close method
input.close();
// Check output
output.on('data', (d) => {
console.log('Input: Data flush received '
+ d.length + ' bytes');
});
console.log("Closed!");
输出:
Closed!
示例 2:
// Node.js program to demonstrate the
// zlib.close() method
// Including zlib and fs module
const zlib = require("zlib");
const fs = require('fs');
// Creating readable Stream
const inp = fs.createReadStream('input.text');
// Creating writable stream
const out = fs.createWriteStream('input.txt.gz');
// Calling createDeflateRaw method
const defR = zlib.createDeflateRaw();
// Calling close method
defR.close();
// Piping
inp.pipe(defR).pipe(out);
console.log("Program Completed!");
输出:
Program Completed!
在这里,管道没有完成,因为 close 方法关闭了隐藏的句柄。
参考: https://nodejs.org/api/zlib.html#zlib_zlib_close_callback