Node.js zlib.bytesWritten 属性
zlib.bytesWritten 属性是 zlib 模块的应用程序编程接口,用于指定在处理字节之前写入引擎的字节数(压缩或解压缩,适用于派生类)。
句法:
zlib.bytesWritten
返回值:它返回写入引擎的字节数。
下面的例子说明了在 Node.js 中zlib.bytesWritten 方法的使用:
示例 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);
});
输出:
21
示例 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);
});
输出:
16
参考: https://nodejs.org/api/zlib.html#zlib_zlib_byteswritten