What is the inflate method of the zlib module in Node.js?

The inflate method of the zlib module in Node.js serves as an APIApplication Programming Interface for data decompression.

The process is illustrated below:

To use the inflate method, you will need to import the zlib module into the program, as shown below:

const zlib = require('zlib');

The prototype of the inflate method is shown below:

inflate(buffer, [,options], callback)

Parameters

The inflate method takes the following parameters:

  • buffer: The data to be compressed. This parameter may be of type Buffer, TypedArray, DataView, ArrayBuffer, or string.

  • options: An optional parameter that represents an options object.

  • callback: The callback function.

Return value

The inflate method returns a decompressed chunk of data.

Example

The code below shows how the inflate method works in Node.js:

const zlib = require('zlib');
// initilaizing uncompressed data
var uncompressed = "Learn zlib with Educative"
// compressing data
zlib.deflate(uncompressed, {chunkSize : 1000}, (error, data) => {
if(!error)
{
console.log("Compressed data:", data)
// decompressing data
zlib.inflate(data, {chunkSize : 1000}, (error, data) => {
if(!error)
{
console.log("Decompressed data:", data)
// convert back to string
console.log("Convert back to string:", data.toString("utf-8"))
}
else
{
// output error
console.log(err);
}
});
}
else
{
// output error
console.log(err);
}
});

Explanation

First, a string that represents the uncompressed chunk of data is initialized.

The deflate method in line 77 proceeds to compress the provided data. In this particular example, the options parameter indicates that data chunks should have a maximum size of 10001000 bytes. The callback parameter holds a function that decompresses the provided data chunk.

The compressed data buffer is provided as a parameter to the inflate method in line 1414 for decompression. Once again, the options parameter is set to have a maximum size of 10001000 bytes. The inflate method proceeds to decompress the provided data chunk. Within the callback function of the inflate method, the decompressed data is converted back into the original string through the utf-8 encoding. The converted string is then output.

Note: You can read up further on the zlib module and its methods here.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved