tar-stream is a streaming tar parser and generator and nothing else. It is streams2 and operates purely using streams which means you can easily extract/parse tarballs without ever hitting the file system.
Note that you still need to gunzip your data if you have a .tar.gz. We recommend using gunzip-maybe in conjunction with this.
npm install tar-stream
tar-stream exposes two streams, pack which creates tarballs and extract which extracts tarballs. To modify an existing tarball use both.
It implementes USTAR with additional support for pax extended headers. It should be compatible with all popular tar distributions out there (gnutar, bsdtar etc)
If you want to pack/unpack directories on the file system check out tar-fs which provides file system bindings to this module.
To create a pack stream use tar.pack() and call pack.entry(header, [callback]) to add tar entries.
vartar=require('tar-stream')varpack=tar.pack()// pack is a streams2 stream// add a file called my-test.txt with the content "Hello World!"pack.entry({name: 'my-test.txt'},'Hello World!')// add a file called my-stream-test.txt from a streamvarentry=pack.entry({name: 'my-stream-test.txt',size: 11},function(err){// the stream was added// no more entriespack.finalize()})entry.write('hello')entry.write(' ')entry.write('world')entry.end()// pipe the pack stream somewherepack.pipe(process.stdout)To extract a stream use tar.extract() and listen for extract.on('entry', (header, stream, next) )
varextract=tar.extract()extract.on('entry',function(header,stream,next){// header is the tar header// stream is the content body (might be an empty stream)// call next when you are done with this entrystream.on('end',function(){next()// ready for next entry})stream.resume()// just auto drain the stream})extract.on('finish',function(){// all entries read})pack.pipe(extract)The tar archive is streamed sequentially, meaning you must drain each entry's stream as you get them or else the main extract stream will receive backpressure and stop reading.
The header object using in entry should contain the following properties.
Most of these values can be found by stat'ing a file.
{name: 'path/to/this/entry.txt',size: 1314,// entry size. defaults to 0mode: 0644,// entry mode. defaults to to 0755 for dirs and 0644 otherwisemtime: newDate(),// last modified date for entry. defaults to now.type: 'file',// type of entry. defaults to file. can be:// file | link | symlink | directory | block-device// character-device | fifo | contiguous-filelinkname: 'path',// linked file nameuid: 0,// uid of entry owner. defaults to 0gid: 0,// gid of entry owner. defaults to 0uname: 'maf',// uname of entry owner. defaults to nullgname: 'staff',// gname of entry owner. defaults to nulldevmajor: 0,// device major version. defaults to 0devminor: 0// device minor version. defaults to 0}Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.
varextract=tar.extract()varpack=tar.pack()varpath=require('path')extract.on('entry',function(header,stream,callback){// let's prefix all names with 'tmp'header.name=path.join('tmp',header.name)// write the new entry to the pack streamstream.pipe(pack.entry(header,callback))})extract.on('finish',function(){// all entries done - lets finalize itpack.finalize()})// pipe the old tarball to the extractoroldTarballStream.pipe(extract)// pipe the new tarball the another streampack.pipe(newTarballStream)varfs=require('fs')vartar=require('tar-stream')varpack=tar.pack()// pack is a streams2 streamvarpath='YourTarBall.tar'varyourTarball=fs.createWriteStream(path)// add a file called YourFile.txt with the content "Hello World!"pack.entry({name: 'YourFile.txt'},'Hello World!',function(err){if(err)throwerrpack.finalize()})// pipe the pack stream to your filepack.pipe(yourTarball)yourTarball.on('close',function(){console.log(path+' has been written')fs.stat(path,function(err,stats){if(err)throwerrconsole.log(stats)console.log('Got file info successfully!')})})See tar-fs for a performance comparison with node-tar
MIT
