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


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

zlib.brotliCompressSync()方法是Zlib模塊的內置應用程序編程接口,用於通過BrotliCompress壓縮數據塊。

用法:

zlib.brotliCompressSync( buffer, options )

參數:此方法接受上述和以下所述的兩個參數:



  • buffer:此參數保存類型為Buffer,TypedArray,DataView,ArrayBuffer,字符串的緩衝區。
  • options:它是一個可選參數。

返回值:它使用BrotliCompress返回數據塊。

以下示例說明了Node.js中zlib.brotliCompressSync()方法的使用:

範例1:

// Node.js program to demonstrate the      
// zlib.brotliCompressSync() method   
  
// Including zlib module 
var zlib = require('zlib'); 
  
// Declaring input and assigning 
// it a value string 
var input = "0123456789"; 
  
// Calling brotliCompressSync method 
var brotliCom = zlib.brotliCompressSync(input); 
  
console.log(brotliCom);

輸出:

// Buffer 8b 04 80 30 31 32 33 34 35 36 37 38 39 03

範例2:

// Node.js program to demonstrate the      
// zlib.brotliCompressSync() method   
  
// Including zlib module 
var zlib = require('zlib'); 
  
// Declaring input and assigning 
// it a value string 
var input = "0123456789"; 
  
// Calling brotliCompressSync method 
var brotliCom = zlib.brotliCompressSync(input).toString('hex'); 
  
console.log(brotliCom);

輸出:

8b04803031323334353637383903

參考: https://nodejs.org/api/zlib.html#zlib_zlib_brotlicompresssync_buffer_options




相關用法


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