[英]How to extract .tar.bz2 in node.js using streams?
我正在尝试在node.js中提取一些.tar.bz2文件。 我在这里搜索npm,github和teh google,但是还没有现成的解决方案。
我的文件各有25mb,所以我认为最好的方法是使用tar
模块通过管道传输流(类似于您如何使用来自node.js内置的Ztar库的.tar.gz中的Gunzip)。 这样,我也可以使用request
直接从管道http中提取。
我找到了https://github.com/Woodya/node-gzbz2 (这是许多重命名的fork,例如gzbz
),但是它们需要使用node-gyp
gzbz
构建外部依赖node-gyp
。 我不想使用这些,因为我要构建的模块必须能够在npm,仅使用npm的linux,mac和Windows上轻松运行,而不必依赖于像python这样的外部库。
或者,我查看https://github.com/cscott/seek-bzip (及其来源),我喜欢它是纯JavaScript的方式,但只解码缓冲区。
有人可以建议我去这里吗?
编辑: seek-bzip
的作者友好地创建了一个包装器,以将其同步流转换为异步流,但是此修复方法取决于node-fibers
,该node-fibers
再次使用node-gyp
,在我看来,这是不可取的。 参见https://github.com/cscott/seek-bzip/issues/1
edit2:我仍在寻找一种跨平台的解决方案,但这是使用CLI命令的一种快速方法:
var cmd = 'bunzip2 -c ' + sourceFile + ' | (cd ' + targetDir + '; tar -xf -)';
require('child_process').exec(cmd, function (err, stdout, stderr) {
if (err) {
// bad
}
// yea!
});
我觉得这个问题实际上是两个问题:如何解密bz2和如何解压缩。 我将回答解皮部分。 tar-stream模块是一个非常不错的模块:
var tar = require('tar-stream')
var extract = tar.extract();
extract.on('entry', function(header, stream, callback) {
// make directories or files depending on the header here...
// call callback() when you're done with this entry
});
fs.createReadStream("something.tar").pipe(extract)
extract.on('finish', function() {
console.log('done!')
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.