import fs from 'fs';
import tar from 'tar';
import bz2 from 'unbzip2-stream';
function extractTarBz2(filePath: string, outputPath: string) {
const inputStream = fs.createReadStream(filePath);
const bz2Stream = bz2();
inputStream.pipe(bz2Stream).pipe(tar.x({C: outputPath}));
bz2Stream.on('error', function() {
console.log('Error extracting tar.bzip2 file');
});
tar.x({C: outputPath}).on('end', function() {
console.log('Extraction complete!');
});
}
extractTarBz2('data.tar.bz2', 'output');
クラス版
import fs from 'fs';
import tar from 'tar';
import bz2 from 'unbzip2-stream';
class Extractor {
private filePath: string;
private outputPath: string;
constructor(filePath: string, outputPath: string) {
this.filePath = filePath;
this.outputPath = outputPath;
}
public extract() {
const inputStream = fs.createReadStream(this.filePath);
const bz2Stream = bz2();
inputStream.pipe(bz2Stream).pipe(tar.x({C: this.outputPath}));
bz2Stream.on('error', function() {
console.log('Error extracting tar.bzip2 file');
});
tar.x({C: this.outputPath}).on('end', function() {
console.log('Extraction complete!');
});
}
}
const extractor = new Extractor('data.tar.bz2', 'output');
extractor.extract();