tar-stream
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
Usage
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)
Related
If you want to pack/unpack directories on the file system check out tar-fs which provides file system bindings to this module.
Packing
To create a pack stream use tar.pack()
and call pack.entry(header, [callback])
to add tar entries.
var tar =var pack = tar // pack is a streams2 stream// add a file called my-test.txt with the content "Hello World!"pack// add a file called my-stream-test.txt from a streamvar entry = packentryentryentryentry// pipe the pack stream somewherepack
Extracting
To extract a stream use tar.extract()
and listen for extract.on('entry', (header, stream, next) )
var extract = tarextractextractpack
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.
Headers
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: // 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
Modifying existing tarballs
Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.
var extract = tarvar pack = tarvar path =extractextract// pipe the old tarball to the extractoroldTarballStream// pipe the new tarball the another streampack
Saving tarball to fs
var fs =var tar =var pack = tar // pack is a streams2 streamvar path = 'YourTarBall.tar'var yourTarball = fs// add a file called YourFile.txt with the content "Hello World!"pack// pipe the pack stream to your filepackyourTarball
Performance
See tar-fs for a performance comparison with node-tar
License
MIT