What is tar-fs?
The tar-fs npm package is a Node.js module that allows you to interact with tarball (.tar) files. It provides functionality to pack and extract tarball files using file system streams. It is a high-level module that makes it easy to create and extract tar files in a Node.js environment.
What are tar-fs's main functionalities?
Packing files into a tarball
This feature allows you to pack a directory into a tarball. The code sample demonstrates how to pack the contents of '/source/directory' into a tarball named 'archive.tar' located at '/destination/'.
const tar = require('tar-fs');
const fs = require('fs');
let pack = tar.pack('/source/directory')
.pipe(fs.createWriteStream('/destination/archive.tar'));
Extracting files from a tarball
This feature allows you to extract the contents of a tarball into a directory. The code sample demonstrates how to extract the contents of 'archive.tar' from '/source/' into the '/destination/directory'.
const tar = require('tar-fs');
const fs = require('fs');
fs.createReadStream('/source/archive.tar')
.pipe(tar.extract('/destination/directory'));
Other packages similar to tar-fs
tar
The 'tar' package is another Node.js module for manipulating tar files. It provides similar functionality to tar-fs, such as packing and extracting tarball files. However, it also includes support for additional features like gzip compression and incremental backups.
archiver
Archiver is a streaming interface for archive generation, supporting ZIP and TAR formats. It offers more format options than tar-fs and includes features like appending to existing archives and setting global archive headers.
tar-fs
Filesystem bindings for 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 in addition to this.
const tar = require('tar-fs')
const fs = require('fs')
tar.pack('./my-directory').pipe(fs.createWriteStream('my-tarball.tar'))
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.
const pack = tar.pack('./my-directory', {
ignore (name) {
return path.extname(name) === '.bin'
}
})
const extract = tar.extract('./my-other-directory', {
ignore (name) {
return path.extname(name) === '.bin'
}
})
const extractFilesDirs = tar.extract('./my-other-other-directory', {
ignore (_, header) {
return header.type !== 'file' && header.type !== 'directory'
}
})
You can also specify which entries to pack using the entries
option
const pack = tar.pack('./my-directory', {
entries: ['file1', 'subdir/file2']
})
If you want to modify the headers when packing/extracting add a map function to the options
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
const pack = tar.pack('./my-directory', {
mapStream (fileStream, header) {
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
const extract = tar.extract('./my-directory', {
dmode: parseInt(555, 8),
fmode: parseInt(444, 8)
})
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.
var extract = tar.extract('./my-directory', {
readable: true,
writable: true,
})
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
tar.pack('source-directory').pipe(tar.extract('dest-directory'))
Use finalize: false
and the finish
hook to
leave the pack stream open for further entries (see
tar-stream#pack
),
and use pack
to pass an existing pack stream.
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