tar-stream
tar-stream is a streaming tar parser and generator and nothing else. It is streams2 and operates purely using streams which means you can easily extract/parse tarballs without ever hitting the file system.
npm install tar-stream
Usage
tar-stream exposes two streams, pack which creates tarballs and extract which extracts tarballs. To modify an existing tarball use both.
Packing
To create a pack stream use tar.pack()
and call pack.entry(header, [callback])
to add tar entries.
var tar = require('tar-stream');
var pack = tar.pack();
pack.entry({ name: 'my-test.txt' }, 'Hello World!');
myStream.pipe(pack.entry({ name: 'my-stream-test.txt' }, function(err) {
}));
pack.finalize();
pack.pipe(process.stdout);
To extract a stream use tar.extract()
and listen for extract.on('entry', header, stream, callback)
var extract = tar.extract();
extract.on('entry', function(header, stream, callback) {
stream.resume();
stream.on('end', function() {
callback();
});
});
extract.on('finish', function() {
});
pack.pipe(extract);
The header object using in entry
should contain the following properties.
Most of these values can be found by stating a file.
{
name: 'path/to/this/entry.txt',
size: 1314,
mode: 0644,
mtime: new Date(),
type: 'file',
linkname: 'path',
uid: 0,
gid: 0,
uname: 'maf',
gname: 'wheel',
}
Modifying existing tarballs
Using tar-stream it is easy to rewrite paths / change modes etc in an existing tarball.
var extract = tar.extract();
var pack = tar.pack();
var path = require('path');
extract.on('entry', function(header, stream, callback) {
header.name = path.join('tmp', header.name);
stream.pipe(pack.entry(header, callback));
});
extract.on('finish', function() {
pack.finalize();
});
oldTarball.pipe(extract);
pack.pipe(newTarball);
License
MIT