Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

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

Adnan Abbas

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.

The zlib module in Node.js provides compression and decompression functionality utilizing Gzip, Deflate/Inflate, and Brotli.

It can be accessed in your program with:

const zlib = require('zlib');

createGzip() is a function of the zlib module that creates a Gzip object.

Syntax

zlib.createGzip( options )

Parameters

  • options is an optional parameter used to provide options to the zlib classes.

Return value

The createGzip() method returns a new Gzip object.

Example

  • In the example below, we use the fs module to create read and write streams.
  • The read stream is initialized with source, which reads a file called input.txt.
  • The write stream is initialized with destination.
  • Then, we create a Gzip object in line 11.
  • Finally, we use a pipeline to read from the source, compress using the gzip object, and write into destination.
  • If this process does not throw an error, it means that the object was created successfully.
index.js
input.txt
const { createGzip } = require('zlib');
const { pipeline } = require('stream');
const {
createReadStream,
createWriteStream
} = require('fs');
const source = createReadStream('input.txt');
const destination = createWriteStream('input.txt.gz');
const gzip = createGzip();
pipeline(source, gzip, destination, (err) => {
if (err) {
console.error('An error occurred:', err);
process.exitCode = 1;
}
});
console.log("Gzip object created")
createGzip in Node

RELATED TAGS

CONTRIBUTOR

Adnan Abbas
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.

Keep Exploring