Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

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

Hammad Nasir

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The unzipSync function in Node.js zlib module

The unzipSync function is a member function of the zlib class in the Node.js zlib module. The unzipSync function uses Unzip to decompress the provided data and returns the result in a Buffer instance. The syntax of the unzipSync function is as follows:

zlib.unzipSync(buffer[,options])

Parameters

  • buffer is the data that is to be unzipped. It can be any of the following types:
    • Buffer
    • TypedArray
    • DataView
    • ArrayBuffer
    • string
  • options is an optional object that contains zlib options.

Return value

  • Returns a Buffer instance that contains the decompressed data.

Description

The unzipSync function decompresses the data provided as the Buffer argument and returns the results in an instance of Buffer.

Note: The difference between unzipSync and unzip is that the latter takes a callback function because the decompression happens on a separate thread. The former takes no callback function because the decompression happens on the same thread that the unzipSync function is called on.

Example usage of the unzipSync function

The following code snippet provides an example of how to use the unzipSync function:

const zlib = require('zlib');
const rawData = 'Hello World!';
const compressedData = zlib.gzipSync(rawData);
const decompressedData = zlib.unzipSync(compressedData);
console.log("Decompressed Data (Buffer):", decompressedData);
console.log("Decompressed Data (String):", decompressedData.toString());

In the example above, the string Hello World! stored in the rawData variable is compressed using the gzipSync function. The compressed data in the compressedData variable is then decompressed using the unzipSync function. The decompressedData is printed on the standard output as a Buffer instance, as well as a string.

RELATED TAGS

node.js

CONTRIBUTOR

Hammad Nasir
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring