basant307/AI_Governance_Project
045
1# tar-stream2 3tar-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.4 5Note that you still need to gunzip your data if you have a `.tar.gz`. We recommend using [gunzip-maybe](https://github.com/mafintosh/gunzip-maybe) in conjunction with this.6 7```8npm install tar-stream9```10 11[](http://travis-ci.org/mafintosh/tar-stream)12[](http://opensource.org/licenses/MIT)13 14## Usage15 16tar-stream exposes two streams, [pack](https://github.com/mafintosh/tar-stream#packing) which creates tarballs and [extract](https://github.com/mafintosh/tar-stream#extracting) which extracts tarballs. To [modify an existing tarball](https://github.com/mafintosh/tar-stream#modifying-existing-tarballs) use both.17 18 19It implementes USTAR with additional support for pax extended headers. It should be compatible with all popular tar distributions out there (gnutar, bsdtar etc)20 21## Related22 23If you want to pack/unpack directories on the file system check out [tar-fs](https://github.com/mafintosh/tar-fs) which provides file system bindings to this module.24 25## Packing26 27To create a pack stream use `tar.pack()` and call `pack.entry(header, [callback])` to add tar entries.28 29``` js30var tar = require('tar-stream')31var pack = tar.pack() // pack is a streams2 stream32 33// add a file called my-test.txt with the content "Hello World!"34pack.entry({ name: 'my-test.txt' }, 'Hello World!')35 36// add a file called my-stream-test.txt from a stream37var entry = pack.entry({ name: 'my-stream-test.txt', size: 11 }, function(err) {38 // the stream was added39 // no more entries40 pack.finalize()41})42 43entry.write('hello')44entry.write(' ')45entry.write('world')46entry.end()47 48// pipe the pack stream somewhere49pack.pipe(process.stdout)50```51 52## Extracting53 54To extract a stream use `tar.extract()` and listen for `extract.on('entry', (header, stream, next) )`55 56``` js57var extract = tar.extract()58 59extract.on('entry', function(header, stream, next) {60 // header is the tar header61 // stream is the content body (might be an empty stream)62 // call next when you are done with this entry63 64 stream.on('end', function() {65 next() // ready for next entry66 })67 68 stream.resume() // just auto drain the stream69})70 71extract.on('finish', function() {72 // all entries read73})74 75pack.pipe(extract)76```77 78The 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.79 80## Headers81 82The header object using in `entry` should contain the following properties.83Most of these values can be found by stat'ing a file.84 85``` js86{87 name: 'path/to/this/entry.txt',88 size: 1314, // entry size. defaults to 089 mode: 0o644, // entry mode. defaults to to 0o755 for dirs and 0o644 otherwise90 mtime: new Date(), // last modified date for entry. defaults to now.91 type: 'file', // type of entry. defaults to file. can be:92 // file | link | symlink | directory | block-device93 // character-device | fifo | contiguous-file94 linkname: 'path', // linked file name95 uid: 0, // uid of entry owner. defaults to 096 gid: 0, // gid of entry owner. defaults to 097 uname: 'maf', // uname of entry owner. defaults to null98 gname: 'staff', // gname of entry owner. defaults to null99 devmajor: 0, // device major version. defaults to 0100 devminor: 0 // device minor version. defaults to 0101}102```103 104## Modifying existing tarballs105 106Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.107 108``` js109var extract = tar.extract()110var pack = tar.pack()111var path = require('path')112 113extract.on('entry', function(header, stream, callback) {114 // let's prefix all names with 'tmp'115 header.name = path.join('tmp', header.name)116 // write the new entry to the pack stream117 stream.pipe(pack.entry(header, callback))118})119 120extract.on('finish', function() {121 // all entries done - lets finalize it122 pack.finalize()123})124 125// pipe the old tarball to the extractor126oldTarballStream.pipe(extract)127 128// pipe the new tarball the another stream129pack.pipe(newTarballStream)130```131 132## Saving tarball to fs133 134 135``` js136var fs = require('fs')137var tar = require('tar-stream')138 139var pack = tar.pack() // pack is a streams2 stream140var path = 'YourTarBall.tar'141var yourTarball = fs.createWriteStream(path)142 143// add a file called YourFile.txt with the content "Hello World!"144pack.entry({name: 'YourFile.txt'}, 'Hello World!', function (err) {145 if (err) throw err146 pack.finalize()147})148 149// pipe the pack stream to your file150pack.pipe(yourTarball)151 152yourTarball.on('close', function () {153 console.log(path + ' has been written')154 fs.stat(path, function(err, stats) {155 if (err) throw err156 console.log(stats)157 console.log('Got file info successfully!')158 })159})160```161 162## Performance163 164[See tar-fs for a performance comparison with node-tar](https://github.com/mafintosh/tar-fs/blob/master/README.md#performance)165 166# License167 168MIT169 