zlib.createDeflate()方法是Zlib模塊的內置應用程序編程接口,用於創建新的Deflate對象。
用法:
zlib.createDeflate( options )
參數:此方法接受單個參數選項,這是一個包含zlib選項的可選參數。
返回值:它返回一個新的Deflate對象。
以下示例說明了Node.js中zlib.createDeflate()方法的使用:
範例1:
// Node.js program to demonstrate the
// createDeflate() 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 createDeflate method
const def = zlib.createDeflate();
// Piping
inp.pipe(def).pipe(out);
console.log("Program Completed!");
輸出:
Program Completed!
範例2:
// Node.js program to demonstrate the
// createDeflate() 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 createDeflate method
const def = zlib.createDeflate();
// Piping
inp.pipe(out).pipe(def);
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_createdeflate_options
相關用法
- Node.js GM blur()用法及代碼示例
- Node.js GM drawRectangle()用法及代碼示例
- Node.js GM sharpen()用法及代碼示例
- Node.js GM drawPolyline()用法及代碼示例
- Node.js GM drawEllipse()用法及代碼示例
- Node.js GM drawCircle()用法及代碼示例
- Node.js GM drawPolygon()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Node.js | zlib.createDeflate() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。