1
1
# tar-fs
2
2
3
- filesystem bindings for [ tar-stream] ( https://github.com/mafintosh/tar-stream ) .
3
+ Filesystem bindings for [ tar-stream] ( https://github.com/mafintosh/tar-stream ) .
4
4
5
5
```
6
6
npm install tar-fs
7
7
```
8
8
9
- [ ![ build status] ( https://secure.travis-ci.org/mafintosh/tar-fs.png )] ( http://travis-ci.org/mafintosh/tar-fs )
10
-
11
9
## Usage
12
10
13
11
tar-fs allows you to pack directories into tarballs and extract tarballs into directories.
14
12
15
13
It doesn't gunzip for you, so if you want to extract a ` .tar.gz ` with this you'll need to use something like [ gunzip-maybe] ( https://github.com/mafintosh/gunzip-maybe ) in addition to this.
16
14
17
15
``` js
18
- var tar = require (' tar-fs' )
19
- var fs = require (' fs' )
16
+ const tar = require (' tar-fs' )
17
+ const fs = require (' fs' )
20
18
21
19
// packing a directory
22
20
tar .pack (' ./my-directory' ).pipe (fs .createWriteStream (' my-tarball.tar' ))
@@ -30,20 +28,20 @@ is also an alias for `filter`. Additionally you get `header` if you use ignore w
30
28
That way you could also filter by metadata.
31
29
32
30
``` js
33
- var pack = tar .pack (' ./my-directory' , {
34
- ignore : function (name ) {
31
+ const pack = tar .pack (' ./my-directory' , {
32
+ ignore (name ) {
35
33
return path .extname (name) === ' .bin' // ignore .bin files when packing
36
34
}
37
35
})
38
36
39
- var extract = tar .extract (' ./my-other-directory' , {
40
- ignore : function (name ) {
37
+ const extract = tar .extract (' ./my-other-directory' , {
38
+ ignore (name ) {
41
39
return path .extname (name) === ' .bin' // ignore .bin files inside the tarball when extracing
42
40
}
43
41
})
44
42
45
- var extractFilesDirs = tar .extract (' ./my-other-other-directory' , {
46
- ignore : function (_ , header ) {
43
+ const extractFilesDirs = tar .extract (' ./my-other-other-directory' , {
44
+ ignore (_ , header ) {
47
45
// pass files & directories, ignore e.g. symlinks
48
46
return header .type !== ' file' && header .type !== ' directory'
49
47
}
@@ -53,23 +51,23 @@ var extractFilesDirs = tar.extract('./my-other-other-directory', {
53
51
You can also specify which entries to pack using the ` entries ` option
54
52
55
53
``` js
56
- var pack = tar .pack (' ./my-directory' , {
54
+ const pack = tar .pack (' ./my-directory' , {
57
55
entries: [' file1' , ' subdir/file2' ] // only the specific entries will be packed
58
56
})
59
57
```
60
58
61
59
If you want to modify the headers when packing/extracting add a map function to the options
62
60
63
61
``` js
64
- var pack = tar .pack (' ./my-directory' , {
65
- map : function (header ) {
62
+ const pack = tar .pack (' ./my-directory' , {
63
+ map (header ) {
66
64
header .name = ' prefixed/' + header .name
67
65
return header
68
66
}
69
67
})
70
68
71
- var extract = tar .extract (' ./my-directory' , {
72
- map : function (header ) {
69
+ const extract = tar .extract (' ./my-directory' , {
70
+ map (header ) {
73
71
header .name = ' another-prefix/' + header .name
74
72
return header
75
73
}
@@ -79,31 +77,31 @@ var extract = tar.extract('./my-directory', {
79
77
Similarly you can use ` mapStream ` incase you wanna modify the input/output file streams
80
78
81
79
``` js
82
- var pack = tar .pack (' ./my-directory' , {
83
- mapStream : function (fileStream , header ) {
80
+ const pack = tar .pack (' ./my-directory' , {
81
+ mapStream (fileStream , header ) {
84
82
// NOTE: the returned stream HAS to have the same length as the input stream.
85
83
// If not make sure to update the size in the header passed in here.
86
84
if (path .extname (header .name ) === ' .js' ) {
87
85
return fileStream .pipe (someTransform)
88
86
}
89
- return fileStream;
87
+ return fileStream
90
88
}
91
89
})
92
90
93
- var extract = tar .extract (' ./my-directory' , {
94
- mapStream : function (fileStream , header ) {
91
+ const extract = tar .extract (' ./my-directory' , {
92
+ mapStream (fileStream , header ) {
95
93
if (path .extname (header .name ) === ' .js' ) {
96
94
return fileStream .pipe (someTransform)
97
95
}
98
- return fileStream;
96
+ return fileStream
99
97
}
100
98
})
101
99
```
102
100
103
101
Set ` options.fmode ` and ` options.dmode ` to ensure that files/directories extracted have the corresponding modes
104
102
105
103
``` js
106
- var extract = tar .extract (' ./my-directory' , {
104
+ const extract = tar .extract (' ./my-directory' , {
107
105
dmode: parseInt (555 , 8 ), // all dirs should be readable
108
106
fmode: parseInt (444 , 8 ) // all files should be readable
109
107
})
@@ -140,9 +138,9 @@ leave the pack stream open for further entries (see
140
138
and use ` pack ` to pass an existing pack stream.
141
139
142
140
``` js
143
- var mypack = tar .pack (' ./my-directory' , {
141
+ const mypack = tar .pack (' ./my-directory' , {
144
142
finalize: false ,
145
- finish : function (sameAsMypack ) {
143
+ finish (sameAsMypack ) {
146
144
mypack .entry ({name: ' generated-file.txt' }, " hello" )
147
145
tar .pack (' ./other-directory' , {
148
146
pack: sameAsMypack
@@ -151,15 +149,6 @@ var mypack = tar.pack('./my-directory', {
151
149
})
152
150
```
153
151
154
-
155
- ## Performance
156
-
157
- Packing and extracting a 6.1 GB with 2496 directories and 2398 files yields the following results on my Macbook Air.
158
- [ See the benchmark here] ( https://gist.github.com/mafintosh/8102201 )
159
-
160
- * tar-fs: 34.261 seconds
161
- * [ node-tar] ( https://github.com/isaacs/node-tar ) : 366.123 seconds (or 10x slower)
162
-
163
152
## License
164
153
165
154
MIT
0 commit comments