Tar
Linux 系统上的解压命令,通常用于解压 .tar.gz 或者 .tgz,二者是等价的
解压
解压到当前目录
$ tar -xzf common.tar.gz解压到指定目录
$ mkdir test # 目录必须存在
$ tar -xzf common.tar.gz -C test查看 tgz 内容
不解压,只查看 tgz 压缩了的文件名
$ tar -xvf common.tar.gznodejs tar
$ npm i tar
通过 pipe 直接解压来自接口的 tgz 流数据,并且写入本地文件系统
import { pipeline } from 'stream';
import { promisify } from 'node:util'
import Tar from 'tar'
const { data: stream } = await axios.get(`${host}/${api}`, {
responseType: 'stream'
});
const pipe = promisify(pipeline)
return pipe(stream, Tar.x({
C: './test',
}))根据多个 path,打成压缩包
import { globSync } from 'glob';
import tar from 'tar';
import fse from 'fs/promise'
const paths = globSync('xxx', { cwd: context })
const tarPackage = tar.create(
{
gzip: true,
cwd: context,
},
paths,
);
tarPackage.pipe(fse.createWriteStream('./test'))解压 Buffer 类型的 tar.gz。
const buf: Buffer = this.ctx.BlobStore.get('xxx')
const tmpPath = resolve(os.tmpDir(), 'compile')
// 先写入文件
const tgzFile = fs.writeFileSync(tmpPath, buf)
// 再创建可读流
const stream = fs.createReadStream(tgzFile)
const pipe = promisify(pipeline)
// 通过和上面一样的 pipe 走到 tar
pipe(stream, Tar.x({
C: './test',
}))