Create a tar archive from a single file with the Observable API
const {existsSync} = require('fs');
const fileToTar = require('file-to-tar');
const subscription = fileToTar('readme.txt', 'archive.tar').subscribe({
complete() {
console.log('`archive.tar` created.');
existsSync('archive.tar'); //=> true
}
});
// Cancel compression
subscription.unsubscribe();
npm install file-to-tar
const fileToTar = require('file-to-tar');
filePath: string
(path of a file to compress)
tarPath: string
(path of the created archive file)
options: Object
Return: Observable
(Kevin Smith's implementation)
When the Observable
is subscribed, it starts to create a tar file from a given file and successively send compression progress to its Observer
.
Every progress object have two properties header
and bytes
. header
is a header of the entry, and bytes
is the total processed size of the compression.
For example you can get the progress as a percentage by (progress.bytes / progress.header.size || 0) * 100
.
fileToTar('my/file', 'my/archive.tar')
.subscribe(({bytes, header}) => {
console.log(`${(bytes / header.size * 100).toFixed(1)} %`);
}, console.error, () => {
console.log('Completed');
});
0.0 %
0.1 %
0.3 %
0.4 %
︙
99.6 %
99.8 %
99.9 %
100.0 %
Completed
You can pass options to tar-fs's pack()
method and fs.createReadStream()
. Note that:
entries
,strip
,filter
andignore
options are not supported.
Additionally, you can use the following:
Type: Stream
A TransformStream
to modify the archive after compression.
For example, pass zlib.createGzip()
and you can create a gzipped tar.
const fileToTar = require('file-to-tar');
const {createGzip} = require('zlib');
const gzipStream = createGzip();
const observable = fileToTar('Untitled.txt', 'Untitled.tar.gz', {
tarTransform: gzipStream
});
- tar-to-file – Inverse of this module. Decompress a single-file tar archive
ISC License © 2017 - 2018 Shinnosuke Watanabe