概要
tar.xzをnode.jsで解凍する必要が発生した。
日本語で検索しても情報が出なかったので書く。
解決方法
decompressのプラグインとしてdecompress-tarxzを使えば良い
必要なパッケージ
- decompress
- decompress-tarxz
以下のコマンドでインストール
$ npm install --save decompress decompress-tarxz
ソース
decompressをrequireして実行すると解凍可能。
解凍後に処理を行いたい場合にはthenの中で実行するしかない。(copyFileに対するcopySyncのような関数はない。)
その際第三引数にてpluginという配列を指定し、その要素としてdecompress-tarxzをrequireした結果を指定。
const decompress = require('decompress');
const decompressTarxz = require('decompress-tarxz');
var tarballPath = '/path/to/hoge.tar.xz';
var outputPath = '/path/to/output';
decompress(
tarballPath, //第一引数にtar.xzのパスを記入
outputPath, //第二引数に出力先のパスを記入
{ //第三引数にオプションを指定。今回はpluginsにてdecompressTarxz()を指定。
plugins:
[
decompressTarxz()
]
}
).then(
function()
{
console.log('解凍後の処理');
}
);
参考
https://www.npmjs.com/package/decompress
https://www.npmjs.com/package/decompress-tarxz