本文实例讲述了Node.js API详解之 zlib模块用法。分享给大家供大家参考,具体如下:
Node.js API详解之 zlib
zlib模块提供通过 Gzip 和 Deflate/Inflate 实现的压缩功能,可以通过这样使用它:
const zlib = require('zlib');压缩或者解压数据流(例如一个文件)通过zlib流将源数据流传输到目标流中来完成:
const gzip = zlib.createGzip();const fs = require('fs');const inp = fs.createReadStream('input.txt');const out = fs.createWriteStream('input.txt.gz');inp.pipe(gzip).pipe(out);zlib 可以用来实现对 HTTP 中定义的 gzip 和 deflate 内容编码机制的支持。
HTTP 的 Accept-Encoding 头字段用来标记客户端接受的压缩编码。
注意: 下面给出的示例大幅简化,用以展示了基本的概念。使用 zlib 编码成本会很高, 结果应该被缓存。
zlib.createDeflate(options)
说明:
创建并返回一个带有给定 options 的新的 Deflate 对象。
可以使用 deflate 压缩数据。
demo:
const zlib = require('zlib');const deflate = zlib.createDeflate();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(deflate) );// Deflate {// _readableState:// ReadableState { ... },// bytesRead: 0,// _handle: Zlib { jsref: [Circular], onerror: [Function: zlibOnError] },// _hadError: false,// _writeState: Uint32Array [ 0, 0 ],// _outBuffer: ,// _outOffset: 0,// _level: -1,// _strategy: 0,// _chunkSize: 16384,// _flushFlag: 0,// _scheduledFlushFlag: 0,// _origFlushFlag: 0,// _finishFlushFlag: 4,// _info: undefined }zlib.createInflate(options)
说明:
创建并返回一个带有给定 options 的新的 Inflate 对象。
Inflate 用于解压一个 deflate 流。
demo:
const zlib = require('zlib');const deflate = zlib.createDeflate();const inflate = zlib.createInflate();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(deflate).pipe(inflate) );zlib.createDeflateRaw(options)
说明:
创建并返回一个带有给定 options 的新的 DeflateRaw 对象.
使用 deflate 压缩数据,并且不附加一个 zlib 头。
demo:
const zlib = require('zlib');const deflateRaw = zlib.createDeflateRaw();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(deflateRaw) );zlib.createInflateRaw(options)
说明:
创建并返回一个带有给定 options 的新的 InflateRaw 对象。
InflateRaw 用于解压一个 raw deflate 流。
demo:
const zlib = require('zlib');const deflateRaw = zlib.createDeflateRaw();const inflateRaw = zlib.createInflateRaw();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(deflateRaw).pipe(inflateRaw) );zlib.createGzip(options)
说明:
创建并返回一个带有给定 options 的新的 Gunzip 对象。
使用 gzip 压缩数据。
demo:
const zlib = require('zlib');const gzip = zlib.createGzip();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(gzip) );zlib.createGunzip(options)
说明:
创建并返回一个带有给定 options 的新的 Gunzip 对象
使用Gunzip解压缩 gzip 流。
demo:
const zlib = require('zlib');const gzip = zlib.createGzip();const gunzip = zlib.createGunzip();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(gzip).pipe(gunzip) );zlib.createUnzip(options)
说明:
创建并返回一个带有给定 options 的新的 Unzip 对象。
Unzip 对象通过自动检测头信息解压 Gzip 或者 Deflate 压缩的流.
demo:
const zlib = require('zlib');const gzip = zlib.createGzip();const unzip = zlib.createUnzip();const fs = require('fs');const inp = fs.createReadStream('a.js');console.log( inp.pipe(gzip).pipe(unzip) );Convenience Methods(简便用法)
说明:
上面我们介绍了各个压缩类的使用。下面介绍一些对应的简便用法。
所有这些方法都将 Buffer, [TypeArray], DataView, 或者字符串作为第一个 参数,
一个回调函数作为可选的第二个参数提供给 zlib 类, 会在 callback(error, result) 中调用.
每一个方法相对应的都有一个接受相同参数, 但是没有回调的 *Sync 版本.
zlib.deflate(buffer [,options],callback)
zlib.deflateSync(buffer [,options])
zlib.inflate(buffer [,options],callback)
zlib.inflateSync(buffer [,options])
zlib.deflateRaw(buffer [,options],callback)
zlib.deflateRawSync(buffer [,options])
zlib.inflateRaw(buffer [,options],callback)
zlib.inflateRawSync(buffer [,options])
zlib.gzip(buffer [,options],callback)
zlib.gzipSync(buffer [,options])
zlib.gunzip(buffer [,options],callback)
zlib.gunzipSync(buffer [,options])
zlib.unzip(buffer [,options],callback)
zlib.unzipSync(buffer [,options])
使用方式如下:
demo:
const input = '.................................';zlib.deflate(input, (err, buffer) => { if (!err) { console.log(buffer.toString('base64')); } else { // 错误处理 }});const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');zlib.unzip(buffer, (err, buffer) => { if (!err) { console.log(buffer.toString()); } else { // 错误处理 }});希望本文所述对大家node.js程序设计有所帮助。