nodejs
  1. nodejs-zlib-module

Node.js ZLIB Module

Node.js provides a built-in module named zlib that provides compression and decompression functionality. The zlib module provides two core functions: zlib.deflate for compression and zlib.inflate for decompression. In this tutorial, we'll discuss how to use the zlib module in Node.js.

Syntax

The syntax for using the zlib module in Node.js is as follows:

const zlib = require('zlib');

// Compressing a buffer
zlib.deflate(buffer, (err, compressedBuffer) => {
  // handle error, if any
  // use the compressedBuffer for further processing
});

// Decompressing a buffer
zlib.inflate(buffer, (err, decompressedBuffer) => {
  // handle error, if any
  // use the decompressedBuffer for further processing
});

Example

Let's see how to use the zlib module to compress and decompress data:

const zlib = require('zlib');
const fs = require('fs');

// Read the content of a file
const input = fs.readFileSync('./input.txt', 'utf8');

// Compress the input
zlib.deflate(input, (err, compressedBuffer) => {
  if (err) {
    console.error(err);
    return;
  }

  // Write the compressed data to a file
  fs.writeFileSync('./output.deflate', compressedBuffer);

  // Decompress the data
  zlib.inflate(compressedBuffer, (err, decompressedBuffer) => {
    if (err) {
      console.error(err);
      return;
    }

    // Write the decompressed data to a file
    fs.writeFileSync('./output.txt', decompressedBuffer.toString('utf8'));
  });
});

In the example above, we first read the content of a file and compress the content using the zlib.deflate function. We then write the compressed data to a file (./output.deflate) and decompress the data using the zlib.inflate function. Finally, we write the decompressed data to a file (./output.txt).

Output

When we run the example code above, it will create two files, output.deflate and output.txt. The output.deflate file will contain compressed data, and the output.txt file will contain the decompressed data.

Explanation

In the example above, we used the zlib module to compress and decompress data. We first read the content of a file and passed it to the zlib.deflate function to compress it. We then wrote the compressed data to a file using the fs.writeFileSync function.

Next, we used the zlib.inflate function to decompress the data. Once the data was decompressed, we wrote it to a file using the fs.writeFileSync function.

Use

The zlib module is useful for compressing and decompressing data in Node.js. This can be useful for reducing the size of data, such as when transferring data over a network.

Important Points

  • The zlib module provides two core functions: zlib.deflate for compression and zlib.inflate for decompression.
  • The zlib module is built-in to Node.js, so no external dependencies are required.
  • Compressed data is typically smaller than the original data, but the amount of compression achieved depends on the data being compressed.

Summary

In this tutorial, we discussed how to use the zlib module in Node.js to compress and decompress data. We covered the syntax, example, output, explanation, use, important points, and summary of the zlib module in Node.js. With this knowledge, you can now use the zlib module to compress and decompress data in your Node.js applications.

Published on: