Node.js zlib.close() Method
Last Updated :
11 Oct, 2021
The zlib.close() method is an inbuilt application programming interface of the Zlib module which is used to close the underlying handle.
Syntax:
zlib.close( callback )
Parameters: This method accepts single parameter callback which holds the callback function.
Below examples illustrate the use of zlib.close() method in Node.js:
Example 1:
const zlib = require( 'zlib' );
const input = zlib.createGzip();
const output = zlib.createGunzip();
input.pipe(output);
input.write( 'GeeksforGeeks' );
input.flush();
input.close();
output.on( 'data' , (d) => {
console.log( 'Input: Data flush received '
+ d.length + ' bytes' );
});
console.log( "Closed!" );
|
Output:
Closed!
Example 2:
const zlib = require( "zlib" );
const fs = require( 'fs' );
const inp = fs.createReadStream( 'input.text' );
const out = fs.createWriteStream( 'input.txt.gz' );
const defR = zlib.createDeflateRaw();
defR.close();
inp.pipe(defR).pipe(out);
console.log( "Program Completed!" );
|
Output:
Program Completed!
Here, piping is not done as close method closes the hidden handle.
Reference: https://nodejs.org/api/zlib.html#zlib_zlib_close_callback
Share your thoughts in the comments
Please Login to comment...