Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

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

Dian Us Suqlain

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'));
    });
});
createUnzip() function in Node.js

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.

RELATED TAGS

node.js

CONTRIBUTOR

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

View all Courses

Keep Exploring