The zlib.bytesWritten property is an application programming interface of the 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.
The below examples illustrate the use of zlib.bytesWritten method in Node.js:
Example 1:
javascript
// Node.js program to demonstrate the // zlib.bytesWritten Property // Including assert and zlib // module const zlib = require( 'zlib' );
const 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
let numberRead = 0;
let buffers = [];
// Creating a zip object
const 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:
javascript
// Node.js program to demonstrate the // zlib.bytesWritten property // Including zlib module const zlib = require( 'zlib' );
// Input to be written const input = Buffer.from( 'NidhiSingh1352' );
// Calling deflateRaw method zlib.deflateRaw(input, (err, buffer) => { // Creating a Deflate object
const 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