source: node_modules/tar-stream/README.md@ 81bc7da

finki-main main
Last change on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago

Initial commit

  • Property mode set to 100644
File size: 5.1 KB
Line 
1# tar-stream
2
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-stream
9```
10
11[![build status](https://secure.travis-ci.org/mafintosh/tar-stream.png)](http://travis-ci.org/mafintosh/tar-stream)
12[![License](https://img.shields.io/badge/license-MIT-blue.svg)](http://opensource.org/licenses/MIT)
13
14## Usage
15
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## Related
22
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## Packing
26
27To create a pack stream use `tar.pack()` and call `pack.entry(header, [callback])` to add tar entries.
28
29``` js
30var tar = require('tar-stream')
31var pack = tar.pack() // pack is a streams2 stream
32
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 stream
37var 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
43entry.write('hello')
44entry.write(' ')
45entry.write('world')
46entry.end()
47
48// pipe the pack stream somewhere
49pack.pipe(process.stdout)
50```
51
52## Extracting
53
54To extract a stream use `tar.extract()` and listen for `extract.on('entry', (header, stream, next) )`
55
56``` js
57var extract = tar.extract()
58
59extract.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
71extract.on('finish', function() {
72 // all entries read
73})
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## Headers
81
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``` 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
106Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.
107
108``` js
109var 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 stream
117 stream.pipe(pack.entry(header, callback))
118})
119
120extract.on('finish', function() {
121 // all entries done - lets finalize it
122 pack.finalize()
123})
124
125// pipe the old tarball to the extractor
126oldTarballStream.pipe(extract)
127
128// pipe the new tarball the another stream
129pack.pipe(newTarballStream)
130```
131
132## Saving tarball to fs
133
134
135``` js
136var fs = require('fs')
137var tar = require('tar-stream')
138
139var pack = tar.pack() // pack is a streams2 stream
140var 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 err
146 pack.finalize()
147})
148
149// pipe the pack stream to your file
150pack.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 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
168MIT
Note: See TracBrowser for help on using the repository browser.