當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Node.js zlib.brotliDecompress()用法及代碼示例


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




相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | zlib.brotliDecompress() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。