📜  Node.js zlib.createDeflateRaw() 方法

📅  最后修改于: 2022-05-13 01:56:22.692000             🧑  作者: Mango

Node.js zlib.createDeflateRaw() 方法

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

句法:

zlib.createDeflateRaw( options )

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

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

下面的例子说明了在 Node.js 中zlib.createDeflateRaw() 方法的使用:

示例 1:

// Node.js program to demonstrate the     
// createDeflateRaw() 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 createDeflateRaw method
const defR = zlib.createDeflateRaw();
  
// Piping
inp.pipe(defR).pipe(out);
console.log("Program Completed!");

输出:

Program Completed!

示例 2:

// Node.js program to demonstrate the     
// createDeflateRaw() 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 createDeflateRaw method
const defR = zlib.createDeflateRaw();
  
// Piping
inp.pipe(out).pipe(defR);
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/SomberMonumentalCad/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_createdeflateraw_options