| 1 | # tar-fs
|
|---|
| 2 |
|
|---|
| 3 | filesystem bindings for [tar-stream](https://github.com/mafintosh/tar-stream).
|
|---|
| 4 |
|
|---|
| 5 | ```
|
|---|
| 6 | npm install tar-fs
|
|---|
| 7 | ```
|
|---|
| 8 |
|
|---|
| 9 | [](http://travis-ci.org/mafintosh/tar-fs)
|
|---|
| 10 |
|
|---|
| 11 | ## Usage
|
|---|
| 12 |
|
|---|
| 13 | tar-fs allows you to pack directories into tarballs and extract tarballs into directories.
|
|---|
| 14 |
|
|---|
| 15 | 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 |
|
|---|
| 17 | ``` js
|
|---|
| 18 | var tar = require('tar-fs')
|
|---|
| 19 | var fs = require('fs')
|
|---|
| 20 |
|
|---|
| 21 | // packing a directory
|
|---|
| 22 | tar.pack('./my-directory').pipe(fs.createWriteStream('my-tarball.tar'))
|
|---|
| 23 |
|
|---|
| 24 | // extracting a directory
|
|---|
| 25 | fs.createReadStream('my-other-tarball.tar').pipe(tar.extract('./my-other-directory'))
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | To ignore various files when packing or extracting add a ignore function to the options. `ignore`
|
|---|
| 29 | is also an alias for `filter`. Additionally you get `header` if you use ignore while extracting.
|
|---|
| 30 | That way you could also filter by metadata.
|
|---|
| 31 |
|
|---|
| 32 | ``` js
|
|---|
| 33 | var pack = tar.pack('./my-directory', {
|
|---|
| 34 | ignore: function(name) {
|
|---|
| 35 | return path.extname(name) === '.bin' // ignore .bin files when packing
|
|---|
| 36 | }
|
|---|
| 37 | })
|
|---|
| 38 |
|
|---|
| 39 | var extract = tar.extract('./my-other-directory', {
|
|---|
| 40 | ignore: function(name) {
|
|---|
| 41 | return path.extname(name) === '.bin' // ignore .bin files inside the tarball when extracing
|
|---|
| 42 | }
|
|---|
| 43 | })
|
|---|
| 44 |
|
|---|
| 45 | var extractFilesDirs = tar.extract('./my-other-other-directory', {
|
|---|
| 46 | ignore: function(_, header) {
|
|---|
| 47 | // pass files & directories, ignore e.g. symlinks
|
|---|
| 48 | return header.type !== 'file' && header.type !== 'directory'
|
|---|
| 49 | }
|
|---|
| 50 | })
|
|---|
| 51 | ```
|
|---|
| 52 |
|
|---|
| 53 | You can also specify which entries to pack using the `entries` option
|
|---|
| 54 |
|
|---|
| 55 | ```js
|
|---|
| 56 | var pack = tar.pack('./my-directory', {
|
|---|
| 57 | entries: ['file1', 'subdir/file2'] // only the specific entries will be packed
|
|---|
| 58 | })
|
|---|
| 59 | ```
|
|---|
| 60 |
|
|---|
| 61 | If you want to modify the headers when packing/extracting add a map function to the options
|
|---|
| 62 |
|
|---|
| 63 | ``` js
|
|---|
| 64 | var pack = tar.pack('./my-directory', {
|
|---|
| 65 | map: function(header) {
|
|---|
| 66 | header.name = 'prefixed/'+header.name
|
|---|
| 67 | return header
|
|---|
| 68 | }
|
|---|
| 69 | })
|
|---|
| 70 |
|
|---|
| 71 | var extract = tar.extract('./my-directory', {
|
|---|
| 72 | map: function(header) {
|
|---|
| 73 | header.name = 'another-prefix/'+header.name
|
|---|
| 74 | return header
|
|---|
| 75 | }
|
|---|
| 76 | })
|
|---|
| 77 | ```
|
|---|
| 78 |
|
|---|
| 79 | Similarly you can use `mapStream` incase you wanna modify the input/output file streams
|
|---|
| 80 |
|
|---|
| 81 | ``` js
|
|---|
| 82 | var pack = tar.pack('./my-directory', {
|
|---|
| 83 | mapStream: function(fileStream, header) {
|
|---|
| 84 | // NOTE: the returned stream HAS to have the same length as the input stream.
|
|---|
| 85 | // If not make sure to update the size in the header passed in here.
|
|---|
| 86 | if (path.extname(header.name) === '.js') {
|
|---|
| 87 | return fileStream.pipe(someTransform)
|
|---|
| 88 | }
|
|---|
| 89 | return fileStream;
|
|---|
| 90 | }
|
|---|
| 91 | })
|
|---|
| 92 |
|
|---|
| 93 | var extract = tar.extract('./my-directory', {
|
|---|
| 94 | mapStream: function(fileStream, header) {
|
|---|
| 95 | if (path.extname(header.name) === '.js') {
|
|---|
| 96 | return fileStream.pipe(someTransform)
|
|---|
| 97 | }
|
|---|
| 98 | return fileStream;
|
|---|
| 99 | }
|
|---|
| 100 | })
|
|---|
| 101 | ```
|
|---|
| 102 |
|
|---|
| 103 | Set `options.fmode` and `options.dmode` to ensure that files/directories extracted have the corresponding modes
|
|---|
| 104 |
|
|---|
| 105 | ``` js
|
|---|
| 106 | var extract = tar.extract('./my-directory', {
|
|---|
| 107 | dmode: parseInt(555, 8), // all dirs should be readable
|
|---|
| 108 | fmode: parseInt(444, 8) // all files should be readable
|
|---|
| 109 | })
|
|---|
| 110 | ```
|
|---|
| 111 |
|
|---|
| 112 | It can be useful to use `dmode` and `fmode` if you are packing/unpacking tarballs between *nix/windows to ensure that all files/directories unpacked are readable.
|
|---|
| 113 |
|
|---|
| 114 | Alternatively you can set `options.readable` and/or `options.writable` to set the dmode and fmode to readable/writable.
|
|---|
| 115 |
|
|---|
| 116 | ``` js
|
|---|
| 117 | var extract = tar.extract('./my-directory', {
|
|---|
| 118 | readable: true, // all dirs and files should be readable
|
|---|
| 119 | writable: true, // all dirs and files should be writable
|
|---|
| 120 | })
|
|---|
| 121 | ```
|
|---|
| 122 |
|
|---|
| 123 | Set `options.strict` to `false` if you want to ignore errors due to unsupported entry types (like device files)
|
|---|
| 124 |
|
|---|
| 125 | To dereference symlinks (pack the contents of the symlink instead of the link itself) set `options.dereference` to `true`.
|
|---|
| 126 |
|
|---|
| 127 | ## Copy a directory
|
|---|
| 128 |
|
|---|
| 129 | Copying a directory with permissions and mtime intact is as simple as
|
|---|
| 130 |
|
|---|
| 131 | ``` js
|
|---|
| 132 | tar.pack('source-directory').pipe(tar.extract('dest-directory'))
|
|---|
| 133 | ```
|
|---|
| 134 |
|
|---|
| 135 | ## Interaction with [`tar-stream`](https://github.com/mafintosh/tar-stream)
|
|---|
| 136 |
|
|---|
| 137 | Use `finalize: false` and the `finish` hook to
|
|---|
| 138 | leave the pack stream open for further entries (see
|
|---|
| 139 | [`tar-stream#pack`](https://github.com/mafintosh/tar-stream#packing)),
|
|---|
| 140 | and use `pack` to pass an existing pack stream.
|
|---|
| 141 |
|
|---|
| 142 | ``` js
|
|---|
| 143 | var mypack = tar.pack('./my-directory', {
|
|---|
| 144 | finalize: false,
|
|---|
| 145 | finish: function(sameAsMypack) {
|
|---|
| 146 | mypack.entry({name: 'generated-file.txt'}, "hello")
|
|---|
| 147 | tar.pack('./other-directory', {
|
|---|
| 148 | pack: sameAsMypack
|
|---|
| 149 | })
|
|---|
| 150 | }
|
|---|
| 151 | })
|
|---|
| 152 | ```
|
|---|
| 153 |
|
|---|
| 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 | ## License
|
|---|
| 164 |
|
|---|
| 165 | MIT
|
|---|