node zlib压缩模块简介

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_39045645/article/details/100141130

压缩:

index.html压缩成index.html.gz

const zlib = require('zlib');

const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('index.html');
const out = fs.createWriteStream('index.html.gz');

inp.pipe(gzip)
  .on('error', () => {
    // 处理错误
  })
  .pipe(out)
  .on('error', () => {
    // 处理错误
  });

解压:

index.html.gz解压为index.html

const zlib = require('zlib');

const gzip = zlib.createGzip();
const fs = require('fs');
const inp = fs.createReadStream('index.html.gz');
const out = fs.createWriteStream('index.html');

inp.pipe(gzip)
  .on('error', () => {
    // 处理错误
  })
  .pipe(out)
  .on('error', () => {
    // 处理错误
  });

猜你喜欢

转载自blog.csdn.net/qq_39045645/article/details/100141130
今日推荐