What is gzipSync() 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');

gzipSync() is a function of the zlib module, which allows compressing a chunk of data.

Syntax

zlib.gzipSync( buffer, options )

Parameters

  • buffer is the data to be compressed. It can be of any type, such as Buffer, TypedArray, DataView, ArrayBuffer, or string.

  • options is used to provide options to the zlib classes.

Return value

The gzipSync() method returns compressed data.

Example

In the following example, we provide an input string to the gzipSync() method, which compresses the input in line 8.

// Including zlib module
const zlib = require("zlib");
//input declaration
var input = "Educative";
// Calling gzip method to compress data
var compressed = zlib.gzipSync(input);
console.log("Compressed data: ",compressed.toString('utf8'));

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved