Skip to content
Related Articles

Related Articles

Improve Article

Node.js | zlib.bytesWritten Property

  • Last Updated : 02 Apr, 2020

The zlib.bytesWritten property is an application programming interface of zlib module which is used to specify the number of bytes written to the engine before the bytes are processed (compressed or decompressed, as proper for the derived class).

Syntax:

zlib.bytesWritten

Return Value: It returns the number of bytes written to the engine.

Below examples illustrate the use of zlib.bytesWritten method in Node.js:

Example 1:






// Node.js program to demonstrate the     
// zlib.bytesWritten Property
  
// Including assert and zlib 
// module
var zlib = require('zlib');
var assert = require('assert');
  
// Input to be written
const input = Buffer.from('0123456789012345678901');
  
// Calling deflate method
zlib.deflate(input, (err, deflatedBuffer) => {
  assert(!err);
  
  // Declaring buffer and numberRead
  var numberRead = 0;
  var buffers = [];
  
  // Creating a zip object
  var stream = zlib.createGzip()
  
    // Data event
    .on('data', function(chunk) {
     buffers.push(chunk);
     numberRead += chunk.length;
    })
  
    // end event
    .on('end', function() {
       
      // Calling bytesWritten property
      console.log(stream.bytesWritten);
       
      })
    .end(deflatedBuffer);
});

Output:

21

Example 2:




// Node.js program to demonstrate the     
// zlib.bytesWritten property
   
// Including zlib module
var zlib = require('zlib');
   
// Input to be written
const input = Buffer.from('NidhiSingh1352');
   
// Calling deflateRaw method
zlib.deflateRaw(input, (err, buffer) => {
   
  // Creating a Deflate object
  var zlibs = zlib.Deflate()
   
    // Data event
    .on('data', function(chunk) {})
   
    // end event
    .on('end', function() {
   
      // Calling bytesWritten property
      console.log(zlibs.bytesWritten);
   
    })
   .end(buffer);
});

Output:

16

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :