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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Closed!

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.