Node.js zlib.gzip()方法
zlib.gzip()方法是 zlib 模块的内置应用编程接口,用于压缩一大块数据。
语法:
zlib.gzip( buffer, options, callback )
参数:该方法接受三个参数,如上所述,如下所述:
- Buffer: it can be buffer, type, data view, array buffer and string type. *option: is an optional parameter for saving zlib options. Callback: It holds the callback function.*
*返回值:*返回压缩后的数据块。
以下示例说明了 Node.js 中 zlib.gzip()方法的使用:
*例 1:*
**```js // Node.js program to demonstrate the // gzip() 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) => {
if (!err) {
console.log(buffer.toString('base64')); } else { console.log(err); } }); console.log("Data Compressed..."); ```**
*输出:*
Data Compressed...
H4sIAAAAAAAAA3NPTc0GAGGRcasEAAAA
*例 2:*
**```js // Node.js program to demonstrate the // gzip() 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) => {
if (!err) {
console.log(buffer.toString('hex')); } else { console.log(err); } }); console.log("Data Compressed..."); ```**
*输出:*
Hint: hit control+c anytime to enter REPL.
Data Compressed...
1f8b0800000000000003734f4dcd0600619171ab04000000
*参考:*T2】https://nodejs . org/API/zlib . html # zlib _ zlib _ gzip _ buffer _ options _ callback
版权属于:月萌API www.moonapi.com,转载请注明出处