| [81bc7da] | 1 | # tar-stream
|
|---|
| 2 |
|
|---|
| 3 | 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.
|
|---|
| 4 |
|
|---|
| 5 | Note 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 | ```
|
|---|
| 8 | npm install tar-stream
|
|---|
| 9 | ```
|
|---|
| 10 |
|
|---|
| 11 | [](http://travis-ci.org/mafintosh/tar-stream)
|
|---|
| 12 | [](http://opensource.org/licenses/MIT)
|
|---|
| 13 |
|
|---|
| 14 | ## Usage
|
|---|
| 15 |
|
|---|
| 16 | tar-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 |
|
|---|
| 19 | It 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 | ## Related
|
|---|
| 22 |
|
|---|
| 23 | If 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 | ## Packing
|
|---|
| 26 |
|
|---|
| 27 | To create a pack stream use `tar.pack()` and call `pack.entry(header, [callback])` to add tar entries.
|
|---|
| 28 |
|
|---|
| 29 | ``` js
|
|---|
| 30 | var tar = require('tar-stream')
|
|---|
| 31 | var pack = tar.pack() // pack is a streams2 stream
|
|---|
| 32 |
|
|---|
| 33 | // add a file called my-test.txt with the content "Hello World!"
|
|---|
| 34 | pack.entry({ name: 'my-test.txt' }, 'Hello World!')
|
|---|
| 35 |
|
|---|
| 36 | // add a file called my-stream-test.txt from a stream
|
|---|
| 37 | var entry = pack.entry({ name: 'my-stream-test.txt', size: 11 }, function(err) {
|
|---|
| 38 | // the stream was added
|
|---|
| 39 | // no more entries
|
|---|
| 40 | pack.finalize()
|
|---|
| 41 | })
|
|---|
| 42 |
|
|---|
| 43 | entry.write('hello')
|
|---|
| 44 | entry.write(' ')
|
|---|
| 45 | entry.write('world')
|
|---|
| 46 | entry.end()
|
|---|
| 47 |
|
|---|
| 48 | // pipe the pack stream somewhere
|
|---|
| 49 | pack.pipe(process.stdout)
|
|---|
| 50 | ```
|
|---|
| 51 |
|
|---|
| 52 | ## Extracting
|
|---|
| 53 |
|
|---|
| 54 | To extract a stream use `tar.extract()` and listen for `extract.on('entry', (header, stream, next) )`
|
|---|
| 55 |
|
|---|
| 56 | ``` js
|
|---|
| 57 | var extract = tar.extract()
|
|---|
| 58 |
|
|---|
| 59 | extract.on('entry', function(header, stream, next) {
|
|---|
| 60 | // header is the tar header
|
|---|
| 61 | // stream is the content body (might be an empty stream)
|
|---|
| 62 | // call next when you are done with this entry
|
|---|
| 63 |
|
|---|
| 64 | stream.on('end', function() {
|
|---|
| 65 | next() // ready for next entry
|
|---|
| 66 | })
|
|---|
| 67 |
|
|---|
| 68 | stream.resume() // just auto drain the stream
|
|---|
| 69 | })
|
|---|
| 70 |
|
|---|
| 71 | extract.on('finish', function() {
|
|---|
| 72 | // all entries read
|
|---|
| 73 | })
|
|---|
| 74 |
|
|---|
| 75 | pack.pipe(extract)
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | 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.
|
|---|
| 79 |
|
|---|
| 80 | ## Headers
|
|---|
| 81 |
|
|---|
| 82 | The header object using in `entry` should contain the following properties.
|
|---|
| 83 | Most of these values can be found by stat'ing a file.
|
|---|
| 84 |
|
|---|
| 85 | ``` js
|
|---|
| 86 | {
|
|---|
| 87 | name: 'path/to/this/entry.txt',
|
|---|
| 88 | size: 1314, // entry size. defaults to 0
|
|---|
| 89 | mode: 0o644, // entry mode. defaults to to 0o755 for dirs and 0o644 otherwise
|
|---|
| 90 | 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-device
|
|---|
| 93 | // character-device | fifo | contiguous-file
|
|---|
| 94 | linkname: 'path', // linked file name
|
|---|
| 95 | uid: 0, // uid of entry owner. defaults to 0
|
|---|
| 96 | gid: 0, // gid of entry owner. defaults to 0
|
|---|
| 97 | uname: 'maf', // uname of entry owner. defaults to null
|
|---|
| 98 | gname: 'staff', // gname of entry owner. defaults to null
|
|---|
| 99 | devmajor: 0, // device major version. defaults to 0
|
|---|
| 100 | devminor: 0 // device minor version. defaults to 0
|
|---|
| 101 | }
|
|---|
| 102 | ```
|
|---|
| 103 |
|
|---|
| 104 | ## Modifying existing tarballs
|
|---|
| 105 |
|
|---|
| 106 | Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.
|
|---|
| 107 |
|
|---|
| 108 | ``` js
|
|---|
| 109 | var extract = tar.extract()
|
|---|
| 110 | var pack = tar.pack()
|
|---|
| 111 | var path = require('path')
|
|---|
| 112 |
|
|---|
| 113 | extract.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 stream
|
|---|
| 117 | stream.pipe(pack.entry(header, callback))
|
|---|
| 118 | })
|
|---|
| 119 |
|
|---|
| 120 | extract.on('finish', function() {
|
|---|
| 121 | // all entries done - lets finalize it
|
|---|
| 122 | pack.finalize()
|
|---|
| 123 | })
|
|---|
| 124 |
|
|---|
| 125 | // pipe the old tarball to the extractor
|
|---|
| 126 | oldTarballStream.pipe(extract)
|
|---|
| 127 |
|
|---|
| 128 | // pipe the new tarball the another stream
|
|---|
| 129 | pack.pipe(newTarballStream)
|
|---|
| 130 | ```
|
|---|
| 131 |
|
|---|
| 132 | ## Saving tarball to fs
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 | ``` js
|
|---|
| 136 | var fs = require('fs')
|
|---|
| 137 | var tar = require('tar-stream')
|
|---|
| 138 |
|
|---|
| 139 | var pack = tar.pack() // pack is a streams2 stream
|
|---|
| 140 | var path = 'YourTarBall.tar'
|
|---|
| 141 | var yourTarball = fs.createWriteStream(path)
|
|---|
| 142 |
|
|---|
| 143 | // add a file called YourFile.txt with the content "Hello World!"
|
|---|
| 144 | pack.entry({name: 'YourFile.txt'}, 'Hello World!', function (err) {
|
|---|
| 145 | if (err) throw err
|
|---|
| 146 | pack.finalize()
|
|---|
| 147 | })
|
|---|
| 148 |
|
|---|
| 149 | // pipe the pack stream to your file
|
|---|
| 150 | pack.pipe(yourTarball)
|
|---|
| 151 |
|
|---|
| 152 | yourTarball.on('close', function () {
|
|---|
| 153 | console.log(path + ' has been written')
|
|---|
| 154 | fs.stat(path, function(err, stats) {
|
|---|
| 155 | if (err) throw err
|
|---|
| 156 | console.log(stats)
|
|---|
| 157 | console.log('Got file info successfully!')
|
|---|
| 158 | })
|
|---|
| 159 | })
|
|---|
| 160 | ```
|
|---|
| 161 |
|
|---|
| 162 | ## Performance
|
|---|
| 163 |
|
|---|
| 164 | [See tar-fs for a performance comparison with node-tar](https://github.com/mafintosh/tar-fs/blob/master/README.md#performance)
|
|---|
| 165 |
|
|---|
| 166 | # License
|
|---|
| 167 |
|
|---|
| 168 | MIT
|
|---|