TonyTakeshi
TonyTakeshi

Reputation: 5929

NodeJS extract tbz file and save to disk

Would like to know how to extract tbz file with Node JS and save to disk. Sample code would be very appreciated.

Tried with something like this:

var zlib = require('zlib');
var fs = require('fs');

var file = fs.createReadStream('/tmp.tbz');
var zip = file.pipe(zlib.createUnzip());

zip.on("data", function(data) {
  console.log(data);
});

zip.on("error",function(error){
    console.log(error);
});

But end up with:

{ [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR' } 

Not sure I did this correctly.

Upvotes: 1

Views: 728

Answers (2)

Slaphead
Slaphead

Reputation: 101

It seems .tbz is bzip2, therefore you can use this module: https://www.npmjs.com/package/unbzip2-stream to extract data.

I was able to extract a .tbz file like this:

fs.createReadStream(sourceFileName)
  .pipe(bz2())
  .pipe(fs.createWriteStream(destinationFileName))

Upvotes: 0

TonyTakeshi
TonyTakeshi

Reputation: 5929

Didn't manage to find node library for extracting the tbz file, as I test with zlib,node-tar and etc. Eventually come out with this hack using 'tar' command.

var util  = require('util'),
    spawn = require('child_process').spawn,
    ls    = spawn('tar', ['-C','/home/tony/Desktop','-xvf', '/home/tony/Desktop/tmp.tbz']);

ls.stdout.on('data', function (data) {
  console.log(data);
});

ls.on('exit', function (code) {
  console.log('child process exited with code ' + code);
});

I temporary accept my answer, until better one comes.

Upvotes: 1

Related Questions