How to use 'zlib.createUnzip()' function in Node.js

Node.js includes a built-in library named zlib that performs compression/decompression on strings and buffers in a program.

We use the zlib.createUnzip() method to create a new Unzip object.

Syntax

zlib.createUnzip( options )

The createUnzip() method takes one optional argument as a parameter, i.e., options, which includes the options that are present for all Zlib objects.

The function returns the newly created unzipped object.

Code

Let’s observe the following implementation of the zlib.createUnzip() module in Node.js.

const zlib = require('zlib');
zlib.gzip('This is a string', function(err, data){
if (err)
{
return;
}
var obj_unzip = zlib.createUnzip();
obj_unzip.write(data);
obj_unzip.on('data', function(data) {
console.log(data.toString('hex'));
console.log(data.toString('base64'));
console.log(data.toString('utf8'));
});
});

Explanation

The above code example shows how to create a new Unzip object using createUnzip(). The zlib.gzip compresses the text passed to it.

The Unzip object created using zlib.createUnzip() uses the data argument from zlib.gzip and performs operations on a text by converting it to hex, base64, and utf8 formats, then displays them on the console.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved