0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JavaScriptでtar.bz2で圧縮されたファイルを展開する

Last updated at Posted at 2023-11-28
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();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?