home PYTHONJAVA
 

Node.js ZLIB


The Node.js Zlib module is utilized to give compression and decompression (compress and unfasten) functionalities. It is implemented utilizing Gzip and empty/swell.

The zlib module can be gotten to using:

  1. const zlib = require('zlib');  

Compressing and decompressing a record should be possible by channeling the source stream information into a goal stream through zlib stream.

Node.js ZLIB Example: Compress File

Let's see a basic case of Node.js ZLIB module to compress a document "input.txt" into "input.txt.gz".

File: zlib_example1.js

  1. const zlib = require('zlib');  
  2. const gzip = zlib.createGzip();  
  3. const fs = require('fs');  
  4. const inp = fs.createReadStream('input.txt');  
  5. const out = fs.createWriteStream('input.txt.gz');  
  6. inp.pipe(gzip).pipe(out);  

We have a text file named "input.txt" on the desktop.

  1. node zlib_example1.js  

You can see that it will simple a compressed file named "input.txt.gz" on the desktop.

Node.js zlib example 3

Node.js ZLIB Example: Decompress File

Let's see a simple example of Node.js ZLIB module to decompress a file "input.txt.gz" into "input2.txt".

File: zlib_example2.js

  1. const zlib = require('zlib');    
  2. const unzip = zlib.createUnzip();  
  3. const fs = require('fs');  
  4. const inp = fs.createReadStream('input.txt.gz');  
  5. const out = fs.createWriteStream('input2.txt');  
  6.   
  7. inp.pipe(unzip).pipe(out);  
  1. node zlib_example2.js  

Now you will see that same code of "input.txt" is available into "input2.txt" file.






welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.