Node.js | zlib.deflate() Method

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

Syntax:

zlib.deflate( buffer, options, callback )

Parameters: This method accept three parameters as mentioned above and described below:

  • buffer: It can be of type Buffer, TypedArray, DataView, ArrayBuffer, and string.
  • options: It is an optional parameter that holds the zlib options.
  • callback: It holds the callback function.

Return Value: It returns the chunk of data after compression.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// deflate() method
  
// Including zlib module
const zlib = require("zlib");
  
// Declaring input and assigning
// it a value string
var input = "Geeks";
  
// Calling deflate method
zlib.deflate(input, (err, buffer) => {
  
  if (!err) {
  
    console.log(buffer.toString('base64'));
  
  else {
    console.log(err);
  }
});
console.log("Data Compressed...");

chevron_right


Output:

Data Compressed...
eJxzT03NLgYABXQB8A==

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the     
// deflate() method
  
// Including zlib module
const zlib = require("zlib");
  
// Declaring input and assigning
// it a value string
var input = "Geeks";
  
// Calling deflate method
zlib.deflate(input, (err, buffer) => {
  
  if (!err) {
  
    console.log(buffer.toString('bas64'));
  
  else {
    console.log(err);
  }
});
console.log("Data Compressed...");

chevron_right


Output:

Data Compressed...
buffer.js:631
  throw new ERR_UNKNOWN_ENCODING(encoding);
  ^

TypeError [ERR_UNKNOWN_ENCODING]: Unknown encoding: bas64
    at stringSlice (buffer.js:631:9)    
    at Buffer.toString (buffer.js:667:10)    
    at Deflate.zlib.deflate [as cb] 
(/home/runner/BeautifulMiserlySourcecode/index.js:17:24)
    at Deflate.zlibBufferOnEnd (zlib.js:133:10)
    at Deflate.emit (events.js:203:15)
    at Deflate.EventEmitter.emit (domain.js:448:20)
    at endReadableNT (_stream_readable.js:1143:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Here, an error occurs in encoding the data so, error is thrown.

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




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.