# tar-fs

> filesystem bindings for tar-stream

Latest version **3.1.3** (published 2026-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install tar-fs
pnpm add tar-fs
yarn add tar-fs
bun add tar-fs
```

## Health

**Score 63/100 (C)** — status: active.

Positive: has types package; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.1.3 |
| Published | 2026-06-27 |
| First published | 2013-12-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/tar-fs) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 18.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 382 |
| Author | Mathias Buus |
| Maintainers | mafintosh, maxogden |
| Keywords | tar, fs, file, tarball, directory, stream |

## Links

- npm: https://www.npmjs.com/package/tar-fs
- Repository: https://github.com/mafintosh/tar-fs
- Issues: https://github.com/mafintosh/tar-fs/issues
- npm.io page: https://npm.io/package/tar-fs

## Dependencies (2)

- [pump](https://npm.io/package/pump.md) ^3.0.0
- [tar-stream](https://npm.io/package/tar-stream.md) ^3.1.5

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 3.1.3 (latest) — 2026-06-27
- 2.1.5 (tmp) — 2026-06-27
- 3.1.2 — 2026-03-04
- 1.16.6 — 2025-09-16
- 2.1.4 — 2025-09-16
- 3.1.1 — 2025-09-16
- 3.1.0 — 2025-06-30
- 3.0.10 — 2025-06-16
- 1.16.5 — 2025-05-22
- 2.1.3 — 2025-05-22
- 3.0.9 — 2025-05-22
- 3.0.8 — 2025-01-14
- 1.16.4 — 2025-01-12
- 2.1.2 — 2025-01-12
- 3.0.7 — 2025-01-12
- … 62 more at https://npm.io/package/tar-fs/versions

## README

# tar-fs

Filesystem bindings for [tar-stream](https://github.com/mafintosh/tar-stream).

```
npm install tar-fs
```

## Usage

tar-fs allows you to pack directories into tarballs and extract tarballs into directories.

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.

``` js
const tar = require('tar-fs')
const fs = require('fs')

// packing a directory
tar.pack('./my-directory').pipe(fs.createWriteStream('my-tarball.tar'))

// extracting a directory
fs.createReadStream('my-other-tarball.tar').pipe(tar.extract('./my-other-directory'))
```

To ignore various files when packing or extracting add a ignore function to the options. `ignore`
is also an alias for `filter`. Additionally you get `header` if you use ignore while extracting.
That way you could also filter by metadata.

``` js
const pack = tar.pack('./my-directory', {
  ignore (name) {
    return path.extname(name) === '.bin' // ignore .bin files when packing
  }
})

const extract = tar.extract('./my-other-directory', {
  ignore (name) {
    return path.extname(name) === '.bin' // ignore .bin files inside the tarball when extracing
  }
})

const extractFilesDirs = tar.extract('./my-other-other-directory', {
  ignore (_, header) {
    // pass files & directories, ignore e.g. symlinks
    return header.type !== 'file' && header.type !== 'directory'
  }
})
```

You can also specify which entries to pack using the `entries` option

```js
const pack = tar.pack('./my-directory', {
  entries: ['file1', 'subdir/file2'] // only the specific entries will be packed
})
```

If you want to modify the headers when packing/extracting add a map function to the options

``` js
const pack = tar.pack('./my-directory', {
  map (header) {
    header.name = 'prefixed/'+header.name
    return header
  }
})

const extract = tar.extract('./my-directory', {
  map (header) {
    header.name = 'another-prefix/'+header.name
    return header
  }
})
```

Similarly you can use `mapStream` incase you wanna modify the input/output file streams

``` js
const pack = tar.pack('./my-directory', {
  mapStream (fileStream, header) {
    // NOTE: the returned stream HAS to have the same length as the input stream.
    // If not make sure to update the size in the header passed in here.
    if (path.extname(header.name) === '.js') {
      return fileStream.pipe(someTransform)
    }
    return fileStream
  }
})

const extract = tar.extract('./my-directory', {
  mapStream (fileStream, header) {
    if (path.extname(header.name) === '.js') {
      return fileStream.pipe(someTransform)
    }
    return fileStream
  }
})
```

Set `options.fmode` and `options.dmode` to ensure that files/directories extracted have the corresponding modes

``` js
const extract = tar.extract('./my-directory', {
  dmode: parseInt(555, 8), // all dirs should be readable
  fmode: parseInt(444, 8) // all files should be readable
})
```

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.

Alternatively you can set `options.readable` and/or `options.writable` to set the dmode and fmode to readable/writable.

``` js
var extract = tar.extract('./my-directory', {
  readable: true, // all dirs and files should be readable
  writable: true, // all dirs and files should be writable
})
```

Set `options.strict` to `false` if you want to ignore errors due to unsupported entry types (like device files)

To dereference symlinks (pack the contents of the symlink instead of the link itself) set `options.dereference` to `true`.

## Copy a directory

Copying a directory with permissions and mtime intact is as simple as

``` js
tar.pack('source-directory').pipe(tar.extract('dest-directory'))
```

## Interaction with [`tar-stream`](https://github.com/mafintosh/tar-stream)

Use `finalize: false` and the `finish` hook to
leave the pack stream open for further entries (see
[`tar-stream#pack`](https://github.com/mafintosh/tar-stream#packing)),
and use `pack` to pass an existing pack stream.

``` js
const mypack = tar.pack('./my-directory', {
  finalize: false,
  finish (sameAsMypack) {
    mypack.entry({name: 'generated-file.txt'}, "hello")
    tar.pack('./other-directory', {
      pack: sameAsMypack
    })
  }
})
```

## License

MIT

---
_Source: https://npm.io/package/tar-fs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
