创建各种用于压缩及解压缩的对象,均为既可用于读取流数据的对象,又可用于写入流数据的对象
zlib.createGzip([options]) - 压缩
zlib.createDeflate([options]) - 压缩
zlib.createDeflateRaw([options]) - 压缩
zlib.createUnzip([options]) - 解压
options - 指定压缩或解压数据时所使用的选项
压缩解压缩代码示例
const zlib = require('zlib')
const fs = require('fs')
// 使用Gzip对象压缩文件
const gzip = zlib.createGzip()
const inp = fs.createReadStream('test.txt')
const out = fs.createWriteStream('test.txt.gz')
inp.pipe(gzip).pipe(out)
// 使用Gunzip对象解压缩文件
const gunzip = zlib.createGunzip()
const inp = fs.createReadStream('test.txt.gz')
const out = fs.createWriteStream('test.txt')
inp.pipe(gunzip).pipe(out)
各种用于压缩或解压缩数据的方法,这些方法不再使用options,使用各种默认选项