What is gunzip() in Node.js zlib module?

The zlib module in Node.js provides compression and decompression functionality utilizing Gzip, Deflate/Inflate, and Brotli.

It can be accessed in your program with:

const zlib = require('zlib');

gunzip() is a function of the zlib module that allows decompressing a stream of data.

Syntax

zlib.gunzip( buffer, options, callback )

Parameters

  • buffer is the data to be decompressed. It can be of any type, such as Buffer, TypedArray, DataView, ArrayBuffer, or string.

  • options is an optional argument that is used to provide options to the zlib classes.

  • callback is the call-back function that is executed.

Return value

The gunzip() method returns decompressed data.

Example

In the following example, we provide an input string to the gzip() method, which compresses the input in line 8. Then, we use the gunzip() method in line 13 to recover the original input string. We provide the compressed data buffer and the callback function as arguments to the gunzip() method.

// Including zlib module
const zlib = require("zlib");
//input declaration
var input = "Educative";
// Calling gzip method to compress data
zlib.gzip(input, (err, buffer) => {
console.log("Compressed data: ",buffer.toString('utf8'));
// Calling gunzip method
zlib.gunzip(buffer, (err, buffer) => {
console.log("Decompressed data: ",buffer.toString('utf8'));
});
});
Copyright ©2024 Educative, Inc. All rights reserved