node js - Node js ZLIB - node - nodejs - node js tutorial - webnode




What is ZLIB in Node.js?

  • zlib is a software library used for data compression.
  • The Node.js Zlib module is used to provide compression and decompression (zip and unzip) functionalities.
  • It is implemented using Gzip and deflate/inflate.
  • The zlib module can be accessed using
const zlib = require('zlib');  
  • Compressing and decompressing a file can be done by piping the source stream data into a destination stream through zlib stream.
 nodejs zlib image

Learn Node js - node js Tutorial - nodejs zlib image - node - Node js Examples

Node.js ZLIB Example: Compress File

  • Let's see a simple example of Node.js ZLIB module to compress a file "input.txt" into "input.txt.gz".
  • Node JS Tutorial File: zlib_example1.js

zlib_example1.js

const zlib = require('zlib');  
const gzip = zlib.createGzip();  
const fs = require('fs');  
const inp = fs.createReadStream('input.txt');  
const out = fs.createWriteStream('input.txt.gz');  
inp.pipe(gzip).pipe(out);  
  • We have a text file named "input.txt" on the desktop.
 text file

Learn Node js - node js Tutorial - text file - node - Node js Examples

  • Open Node.js command prompt and run the following code:
  • node zlib_example1.js
 nodejs zlib example

Learn Node js - node js Tutorial - nodejs zlib example - node - Node js Examples

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

Learn Node js - node js Tutorial - nodejs input file - node - Node js Examples

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".
  • Node JS Tutorial File: zlib_example2.js

zlib_example2.js

const zlib = require('zlib');    
const unzip = zlib.createUnzip();  
const fs = require('fs');  
const inp = fs.createReadStream('input.txt.gz');  
const out = fs.createWriteStream('input2.txt');  
  
inp.pipe(unzip).pipe(out);  
  • Now you will see that same code of "input.txt" is available into "input2.txt" file.

This wikitechy technological portal provides you whole lot of information related to the topics such as mongodb tutorial , what is node js , express js tutorial , node js tutorial pdf , learn node js , node js tutorial for beginners , node js tutorial w3schools , node js express , javascript for beginners , node js express tutorial , node js tutorial point , node js book , node js server , note js , node js mongodb , what is node js used for , why node js , node tutorial , node js basics , tutorialspoint node js , node express , node js sample application , nodeschool , node js for beginners , express tutorial , node js application , use of node js , npm tutorial , node js org , node js training , node js online training , node js tutorial beginner , how to use node js , node js hello world , express node js , node js mongodb tutorial , nodejs http , why use node js

Related Searches to Node.js ZLIB