Skip to content

Instantly share code, notes, and snippets.

@mcollina
Created June 19, 2015 10:57
Show Gist options
  • Save mcollina/7c87771b94958b714bd4 to your computer and use it in GitHub Desktop.
Save mcollina/7c87771b94958b714bd4 to your computer and use it in GitHub Desktop.
Appending to a tar file
var tar = require('tar-stream')
var concat = require('concat-stream')
var assert = require('assert')
var pack1 = tar.pack()
pack1.entry({
name: 'hello'
}, 'hello world')
pack1.finalize()
pack1.pipe(concat(function (buf1) {
var extract = tar.extract()
var pack2 = tar.pack()
extract.on('entry', function(header, stream, callback) {
// write the new entry to the pack stream
stream.pipe(pack2.entry(header, callback))
})
extract.on('finish', function() {
pack2.entry({
name: 'addition'
}, 'a file addition')
// all entries done - lets finalize it
pack2.finalize()
})
// pipe the old tarball to the extractor
extract.end(buf1)
// pipe the new tarball the another stream
pack2.pipe(concat(function (buf2) {
for (var i = 0; i < buf1.length; i++) {
if (buf1[i] !== buf2[i]) {
console.log('the start being different at position', i)
break
}
}
assert.equal(buf1.length, 2048, 'the first tar file must be 4 blocks long')
assert.equal(buf2.length, 3072, 'the second tar file must be 6 blocks long')
assert.deepEqual(buf1.slice(0, 1024), buf2.slice(0, 1024), 'the beginning of the tar files must be the same')
}))
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment