What is createGzip() in Node.js zlib module?
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
optionsis an optional parameter used to provide options to thezlibclasses.
Return value
The createGzip() method returns a new Gzip object.
Example
- In the example below, we use the
fsmodule to create read and write streams. - The read stream is initialized with
source, which reads a file calledinput.txt. - The write stream is initialized with
destination. - Then, we create a
Gzipobject in line 11. - Finally, we use a pipeline to read from the
source, compress using thegzipobject, and write intodestination. - 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")
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved