Tar-stream is a highly efficient tool that allows for streaming tar parsing and generation. It doesn't perform any other function, keeping its operation precise and specialized. It leverages the power of streams enabling easy extraction and parsing of tarballs without involving the file system. In spite of its capabilities, if your data is in the .tar.gz
format, you would still need to gunzip it. Tar-stream is recommended to be used in conjunction with gunzip-maybe in such situations.
To use tar-stream in your projects, you need to install it by running npm install tar-stream
. Post-installation, tar-stream provides you with two streams: the pack stream to create tarballs and the extract stream to extract tarballs. To modify existing tarballs, you can use both streams together.
Pack function usage example:
const tar = require('tar-stream')
const pack = tar.pack() // pack is a stream
// add a file called my-test.txt with the content "Hello World!"
pack.entry({ name: 'my-test.txt' }, 'Hello World!')
// pipe the pack stream
pack.pipe(process.stdout)
Extract function usage example:
const extract = tar.extract()
extract.on('entry', function (header, stream, next) {
// extracting the content body (might be an empty stream)
// calling next() when done with this entry
stream.on('end', function () {
next() // ready for next entry
})
stream.resume() // autodraining the stream
})
extract.on('finish', function () {
// all entries read
})
pack.pipe(extract)
The comprehensive documentation for tar-stream is provided in its README on the GitHub page. The README details the usage of the two primary extracts and packs streams and provides ample examples to understand the functionality. There are also notes regarding the header properties, as well as instructions on how to modify existing tarballs. Examples on saving tarball to file system are provided for further understanding.