Node.js zlib.close() Method

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:




// 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('w3wiki');
  
// 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!");


Output:

Closed!

Example 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!");


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