Open In App

Node.js zlib.unzip() Method

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Syntax:

zlib.unzip( 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. 

The below examples illustrate the use of zlib.unzip() method in Node.js:

Example 1: 

javascript




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


Output:

Data Decompressed...
GfG

Example 2: 

javascript




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


Output:

Data Decompressed...
R2ZH

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads