当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js zlib.createBrotliCompress()用法及代码示例


zlib.createBrotliCompress()方法是Zlib模块的内置应用程序编程接口,用于创建BrotliCompress对象。

用法:

zlib.createBrotliCompress( options )

参数:此方法接受单个参数选项,这是一个包含zlib选项的可选参数。



返回值:它返回一个新的BrotliCompress对象。

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

范例1:

// Node.js program to demonstrate the      
// createBrotliCompress() method 
  
// Including zlib and fs module 
const zlib = require("zlib"); 
const fs = require('fs'); 
  
// Creating readable Stream 
const inp = fs.createReadStream('input.txt'); 
  
// Creating writable stream 
const out = fs.createWriteStream('input.txt.gz'); 
  
// Calling createBrotliCompress method 
const brot = zlib.createBrotliCompress(); 
  
// Piping 
inp.pipe(brot).pipe(out); 
console.log("Program Completed!");

输出:

Program Completed!

范例2:

// Node.js program to demonstrate the      
// createBrotliCompress() method 
  
// Including zlib and fs module 
const zlib = require("zlib"); 
const fs = require('fs'); 
  
// Creating readable Stream 
const inp = fs.createReadStream('input.txt'); 
  
// Creating writable stream 
const out = fs.createWriteStream('input.txt.gz'); 
  
// Calling createBrotliCompress method 
const brot = zlib.createBrotliCompress(); 
  
// Piping 
inp.pipe(out).pipe(brot); 
console.log("Program Completed!");

输出:

Error [ERR_STREAM_CANNOT_PIPE]:Cannot pipe, not readable
    at WriteStream.Writable.pipe (_stream_writable.js:243:24)
    at /home/runner/CorruptFlawedSoftwaresuite/index.js:19:15
    at Script.runInContext (vm.js:133:20)
    at Object. (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

在此,配管未按正确的顺序进行,因此会引发错误。

参考: https://nodejs.org/api/zlib.html#zlib_zlib_createbrotlicompress_options




相关用法


注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Node.js | zlib.createBrotliCompress() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。