1.

Explain chaining the streams?

Answer»

Chaining is

  • a mechanism to connect output of one stream to ANOTHER stream and create a chain of MULTIPLE stream operations.
  • normally used with piping operations.

Example: Create a text file dataSinput.txt with the following content.

  • Monday is first day of week
  • Tuesday is second day of week
  • Wednesday is third day of week

After executing following code.

var fs = require("fs"); //import fs module var zlib = require("zlib"); //import zlib module //creating a readstream to read our inputdatafile dataSinput.txt var readStream = fs.createReadStream("F://dataSinput.txt"); //create a COMPRESSED folder zlib var czlib = zlib.createGzip(); //creating a writestream(initially empty) which is destination for transferred data  var writeStream = fs.createWriteStream("F://dataSoutput.txt.gz"); //Use Pipe command to transfer from readstream to gzip.  //Pipe commands takes all data from readstream and pushes it to compressed writestream file   readStream.pipe(czlib).pipe(writeStream); console.log("File Compressed");

You get result as “File Compressed”. And compressed file dataSoutput.txt.gz as output Which CONSISTS of text file dataSoutput.txt.



Discussion

No Comment Found