文件压缩
const zlib = require('zlib');
const fs = require('fs');
//获取压缩的源文件和目标文件
let src;
let dst;
if (process.argv[2]) {
src = process.argv[2];
} else {
throw new Error('请指定源文件');
}
if (process.argv[3]) {
dst = process.argv[3];
} else {
throw new Error('请指定目标文件');
}
fs.createReadStream(src).pipe(zlib.createGzip()).pipe(fs.createWriteStream(dst));
console.log('压缩完成');
实现效果如下:
文件解压缩
//解压缩
//导入模块
const fs = require('fs');
const zlib = require('zlib');
//判断 存在参赛
if (!process.argv[2] && !process.argv[3]) {
throw new Error('请指定参赛');
}
//管道操作
fs.createReadStream(process.argv[2]).pipe(zlib.createGunzip()).pipe(fs.createWriteStream(process.argv[3]));
console.log('解压成功');