zlib.brotliDecompress()方法是Zlib模块的内置应用程序编程接口,用于通过BrotliCompress解压缩数据块。
用法:
zlib.brotliDecompress( buffer, options, callback )
参数:此方法接受上述和以下所述的三个参数:
- buffer:它可以是Buffer,TypedArray,DataView,ArrayBuffer和字符串的类型。
- options:它是一个可选参数,其中包含zlib选项。
- callback:它包含回调函数。
返回值:解压缩后,它将返回数据块。
以下示例说明了Node.js中zlib.brotliDecompress()方法的使用:
范例1:
// Node.js program to demonstrate the
// brotliDecompress() method
// Including zlib module
const zlib = require("zlib");
// Declaring input and assigning
// it a value string
var input = "Computer Science";
// Calling brotliCompress method
zlib.brotliCompress(input, (err, buffer) => {
// Calling brotliDecompress
zlib.brotliDecompress(buffer, (err, buffer) => {
console.log(buffer.toString('base64'));
});
});
输出:
Q29tcHV0ZXIgU2NpZW5jZQ==
范例2:
// Node.js program to demonstrate the
// brotliDecompress() method
// Including zlib module
const zlib = require("zlib");
// Declaring input and assigning
// it a value string
var input = "Computer Science";
// Calling brotliCompress method
zlib.brotliCompress(input, (err, buffer) => {
// Calling brotliDecompress
zlib.brotliDecompress(buffer, (err, buffer) => {
console.log(buffer.toString('bas64'));
});
});
输出:
buffer.js:631 throw new ERR_UNKNOWN_ENCODING(encoding); ^ TypeError [ERR_UNKNOWN_ENCODING]:Unknown encoding:bas64 at stringSlice (buffer.js:631:9) at Buffer.toString (buffer.js:667:10) at BrotliDecompress.zlib.brotliDecompress [as cb] (/home/runner/BeautifulMiserlySourcecode/index.js:18:28) at BrotliDecompress.zlibBufferOnEnd (zlib.js:133:10) at BrotliDecompress.emit (events.js:203:15) at BrotliDecompress.EventEmitter.emit (domain.js:448:20) at endReadableNT (_stream_readable.js:1143:12) at process._tickCallback (internal/process/next_tick.js:63:19)?
在这里,编码时会发生错误,因此会引发错误。
参考: https://nodejs.org/api/zlib.html#zlib_zlib_brotlidecompress_buffer_options_callback
相关用法
- Node.js GM blur()用法及代码示例
- Node.js GM drawRectangle()用法及代码示例
- Node.js GM sharpen()用法及代码示例
- Node.js GM drawPolyline()用法及代码示例
- Node.js GM drawEllipse()用法及代码示例
- Node.js GM drawCircle()用法及代码示例
- Node.js GM drawPolygon()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | zlib.brotliDecompress() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。