Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags


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

Dian Us Suqlain

In this shot, we learn how to decompress a string or a buffer using the zlib module in Node.js.

The zlib.unzip() function is a built-in module in Node.js that accepts, at most, three distinct parameters.


The zlib.unzip() function looks like this when implemented in a program:

zlib.unzip(buffer, options, callback)
  • buffer: We use buffers to represent a fixed-length sequence of bytes. In the unzip() module, we can include buffers of <TypedArray>, <DataView>, <ArrayBuffer>, and <string> type.

  • options: These are optional objects that we can include in unzip() as a parameter.

  • callback: This is a function that is executed upon the method returning.

zlib.unzip() module


Let’s look at the following implementation of the zlib.unzip() module in Node.js.

var zlib = require("zlib");

var text1 = "Hello this is a string";

zlib.gzip(text1, (err, buffer) => {
	zlib.unzip(buffer, (err, buffer) => {
		console.log("Decompressed string in base64 format:");

var text2 = "Hello this is another string";

zlib.gzip(text2, (err, buffer) => {
	zlib.unzip(buffer, (err, buffer) => {
		console.log("\nDecompressed string in utf8 format:");
Using zlib.unizp() to display string data.


The above code example imports the necessary library zlib required to execute the following code appropriately.

The zlib.unzip() takes three parameters, as mentioned previously. This function takes a text, an err (error parameter), and a buffer.

text1 is decompressed into base64 to print an unreadable message, as shown on the console. text2 is displayed in utf-8 format.

Note: The == sequence in the output generated for text1 indicates padding of one byte when a 24-bit section is not complete. If it contained =, it would mean a padding of two bytes.




Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved

View all Courses

Keep Exploring