Node.js | zlib.gunzip() Method

The zlib.gunzip() method is an inbuilt application programming interface of the Zlib module which is used to decompress a chunk of data.

Syntax:

zlib.gunzip( buffer, options, callback )

Parameters: This method accepts three parameters as mentioned above and described below:

  • buffer: It can be of type Buffer, TypedArray, DataView, ArrayBuffer, and string.
  • options: It is an optional parameter that holds the zlib options.
  • callback: It holds the callback function.

Return Value: It returns the chunk of data after decompression.

Below examples illustrate the use of zlib.gunzip() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// gunzip() method
  
// Including zlib module
const zlib = require("zlib");
  
// Declaring input and assigning
// it a value string
var input = "Geek";
  
// Calling gzip method
zlib.gzip(input, (err, buffer) => {
  
  // Calling gunzip method
 zlib.gunzip(buffer, (err, buffer) => {
  
    console.log(buffer.toString('utf8'));
      
    });
 });
  
console.log("Data Decompressed...");

chevron_right


Output:

Data Decompressed...
Geek

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// gunzip() method
  
// Including zlib module
const zlib = require("zlib");
  
// Declaring input and assigning
// it a value string
var input = "Geek";
  
// Calling gzip method
zlib.gzip(input, (err, buffer) => {
  
  // Calling gunzip method
 zlib.gunzip(buffer, (err, buffer) => {
  
    console.log(buffer.toString('hex'));
      
    });
 });
  
console.log("Data Decompressed...");

chevron_right


Output:

Data Decompressed...
4765656b

Reference: https://nodejs.org/api/zlib.html#zlib_zlib_gunzip_buffer_options_callback




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


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