javascript - Node: adding a string as a file into a tar archive (without temporary files) - Stack Overflow

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

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?

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?

Share Improve this question edited Feb 12, 2014 at 14:40 mikemaccana asked Feb 12, 2014 at 14:28 mikemaccanamikemaccana 124k110 gold badges432 silver badges534 bronze badges 1
  • How to create stream from string: stackoverflow./questions/12755997/… – Hector Correa Commented Feb 12, 2014 at 14:35
Add a ment  | 

2 Answers 2

Reset to default 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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745111319a4611869.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信