Node.js zlib.gunzip() 方法
zlib.gunzip() 方法是 Zlib 模块的内置应用程序编程接口,用于解压缩数据块。
句法:
zlib.gunzip( buffer, options, callback )
参数:此方法接受三个参数,如上所述,如下所述:
- buffer:可以是 Buffer、TypedArray、DataView、ArrayBuffer 和字符串类型。
- options:它是一个可选参数,包含 zlib 选项。
- 回调:它保存回调函数。
返回值:返回解压后的数据块。
下面的例子说明了在 Node.js 中zlib.gunzip() 方法的使用:
示例 1:
// Node.js program to demonstrate the
// gunzip() method
// Including zlib module
const zlib = require("zlib");
// Declaring input and assigning
// it a value string
var input = "Geek";
// Calling gzip method
zlib.gzip(input, (err, buffer) => {
// Calling gunzip method
zlib.gunzip(buffer, (err, buffer) => {
console.log(buffer.toString('utf8'));
});
});
console.log("Data Decompressed...");
输出:
Data Decompressed...
Geek
示例 2:
// Node.js program to demonstrate the
// gunzip() method
// Including zlib module
const zlib = require("zlib");
// Declaring input and assigning
// it a value string
var input = "Geek";
// Calling gzip method
zlib.gzip(input, (err, buffer) => {
// Calling gunzip method
zlib.gunzip(buffer, (err, buffer) => {
console.log(buffer.toString('hex'));
});
});
console.log("Data Decompressed...");
输出:
Data Decompressed...
4765656b
参考: https://nodejs.org/api/zlib.html#zlib_zlib_gunzip_buffer_options_callback