3

I would like to make a tar archive in node. The canonical tar archiving example is based on a file stream:

fstream
  .Reader({path: 'src', type: "Directory"})
  .pipe(tar.Pack())
  .pipe(zlib.createGzip())
  .pipe(fstream.Writer("output.tar.gz"));

My data is actually in memory as a string, which I would like to write to the archive without having to make a temporary file in between.

Is there some way I can do this, e.g. maybe make an fstream out of a string?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • How to create stream from string: http://stackoverflow.com/questions/12755997/how-to-create-streams-from-string-in-node-js – Hector Correa Feb 12 '14 at 14:35

2 Answers2

5

The tar-stream module can do this too:

var tar = require('tar-stream')    

var content = "whatever string you have"
var pack = tar.pack()
pack.entry({name: 'src', type: 'directory'})
pack.entry({name: 'src/someFile.txt'}, content) // file
pack.finalize()

tar-stream can also use streams as sources for file contents.

B T
  • 57,525
  • 34
  • 189
  • 207
1

The tar-async module handles this quite simply, without needing to make fake streams:

var uploadArchive = new Tar({output: fs.createWriteStream('archive.tar')});
uploadArchive.append('somefile.txt', 'someString', function() {
  tape.close();
});
mikemaccana
  • 110,530
  • 99
  • 389
  • 494