Node.js | zlib.gzipSync() Method

The zlib.gzipSync() method is an inbuilt application programming interface of the Zlib module which is used to compress a chunk of data with Gzip.

Syntax:

zlib.gzipSync( buffer, options )

Parameters: This method accepts two parameters as mentioned above and described below:

  • buffer: This parameter holds the buffer of type Buffer, TypedArray, DataView, ArrayBuffer, string.
  • options: This parameter holds the zlib options value.

Return Value: It returns the chunk of data with Gzip.

Below examples illustrate the use of zlib.gzipSync() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// zlib.gzipSync() method  
  
// Including zlib module
var zlib = require('zlib');
  
// Declaring input and assigning
// it a value string
var input = "Nidhi";
  
// Calling brotliDecompressSync method
var gzi = zlib.gzipSync(input);
  
console.log(gzi);

chevron_right


Output:

<Buffer 1f 8b 08 00 00 00 00 00 00 03 f3 cb
    4c c9 c8 04 00 13 f2 5b b1 05 00 00 00>

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// zlib.gzipSync() method  
  
// Including zlib module
var zlib = require('zlib');
  
// Declaring input and assigning
// it a value string
var input = "Nidhi";
  
// Calling brotliDecompressSync method
var gzi = zlib.gzipSync(input).toString('hex');
  
console.log(gzi);

chevron_right


Output:

1f8b0800000000000003f3cb4cc9c8040013f25bb105000000

Reference: https://nodejs.org/api/zlib.html#zlib_zlib_gzipsync_buffer_options

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.