node.js压缩和解压缩

推荐一个极其简单、及其好用的node.js的压缩和解压缩类库  compressing

支持格式: tar、gzip、tgz、zip

以zip为例,tar,tgz和gzip与zip相同。

压缩文件:

const compressing = require('compressing');
compressing.zip.compressFile('logs/info-2018-06-07.log', 'logs/test.zip').then(() => {}).catch(() => {});

//或者
await compressing.zip.compressFile('logs/info-2018-06-07.log', 'logs/test.zip')

压缩文件夹:

const compressing = require('compressing');
compressing.zip.compressDir('logs', 'test3.zip').then(()=>{}).catch(()=>{});

//或者
await compressing.zip.compressDir('logs', 'test3.zip')

使用Stream压缩多个文件(非常强大)

const fs=require('fs');
const compressing = require('compressing');
const zipStream = new compressing.zip.Stream();
zipStream.addEntry('./logs/error-2018-06-07.log')
zipStream.addEntry('./logs/info-2018-06-07.log')

const destStream = fs.createWriteStream('test4.zip');

const pipe = require('multipipe');
pipe(zipStream, destStream, (err) => {
    console.log(err);
})

Stream可以是dir、file、buffer、stream

// dir
zipStream.addEntry('dir/path/to/compress');

// file
zipStream.addEntry('file/path/to/compress');

// buffer
zipStream.addEntry(buffer);

// stream
zipStream.addEntry(stream);

解压缩:

const compressing = require('compressing');
compressing.zip.uncompress('test3.zip', 'test3').then(() => {}).catch(() => {});
//或者
await compressing.zip.uncompress('test3.zip', 'test3') //如果要解压到当前目录换成 './'

 

更多内容:

https://github.com/node-modules/compressing

https://www.npmjs.com/package/compressing

转载于:https://www.cnblogs.com/xbblogs/p/9156446.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值