节点:添加一个string作为一个文件到一个tar档案(没有临时文件)

我想在节点中做一个tar归档。 规范的tar归档示例基于文件stream:

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

我的数据实际上是作为一个string存储在内存中,我想写入存档,而不必在两者之间build立一个临时文件。

有什么办法可以做到这一点,例如也许从string中产生fstream?

焦油stream模块也可以这样做:

 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也可以使用stream作为文件内容的来源。

tar-async模块处理这个很简单,不需要做假stream:

 var uploadArchive = new Tar({output: fs.createWriteStream('archive.tar')}); uploadArchive.append('somefile.txt', 'someString', function() { tape.close(); });