Skip to content

Create a tar archive from a single file

License

Notifications You must be signed in to change notification settings

shinnn/file-to-tar

Repository files navigation

file-to-tar

npm version Build Status Coverage Status

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();

Installation

Use npm.

npm install file-to-tar

API

const fileToTar = require('file-to-tar');

fileToTar(filePath, tarPath [, options])

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

Options

You can pass options to tar-fs's pack() method and fs.createReadStream(). Note that:

Additionally, you can use the following:

tarTransform

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
});

Related project

  • tar-to-file – Inverse of this module. Decompress a single-file tar archive

License

ISC License © 2017 - 2018 Shinnosuke Watanabe