1、对文件进行压缩和解压(创建流)
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
function gzip(_src) {//压缩
fs.stat(_src, function (err, stats) {
if (stats.isFile()) {
let rs = fs.createReadStream(_src);
_dst = path.join(__dirname, path.basename(_src) + '.gz');
rs.pipe(zlib.createGzip()).pipe(fs.createWriteStream(_dst));
}
})
}
function ungzip(_src) {//解压
fs.stat(_src, function (err, stats) {
if (stats.isFile()) {
let rs = fs.createReadStream(_src);
_dst = path.join(__dirname, path.basename(_src, '.gz'));
rs.pipe(zlib.createGunzip()).pipe(fs.createWriteStream(_dst));
}
})
}
网友评论