简体   繁体   English

如何使用流在node.js中提取.tar.bz2?

[英]How to extract .tar.bz2 in node.js using streams?

I'm trying to extract some .tar.bz2 files in node.js. 我正在尝试在node.js中提取一些.tar.bz2文件。 I'm searching here, in npm, github and teh google for this but there is no ready solution. 我在这里搜索npm,github和teh google,但是还没有现成的解决方案。

My files are ~25mb each so I think the best way would be in a piped stream with the tar module (similar to how you use the Gunzip from node.js's built-in ZLib library for .tar.gz). 我的文件各有25mb,所以我认为最好的方法是使用tar模块通过管道传输流(类似于您如何使用来自node.js内置的Ztar库的.tar.gz中的Gunzip)。 This way I can also extract directly from piped http using request . 这样,我也可以使用request直接从管道http中提取。

I found https://github.com/Woodya/node-gzbz2 (and it's many renamed forks like gzbz ) but they require external dependencies build using node-gyp . 我找到了https://github.com/Woodya/node-gzbz2 (这是许多重命名的fork,例如gzbz ),但是它们需要使用node-gyp gzbz构建外部依赖node-gyp I don't want to use these since the module I'm building has to work without hassle on linux, mac and windows using just npm and without depending on external libraries like python. 我不想使用这些,因为我要构建的模块必须能够在npm,仅使用npm的linux,mac和Windows上轻松运行,而不必依赖于像python这样的外部库。

Alternately I look at https://github.com/cscott/seek-bzip (and it's sources) and I like how it is pure javascript but it only decodes Buffers. 或者,我查看https://github.com/cscott/seek-bzip (及其来源),我喜欢它是纯JavaScript的方式,但只解码缓冲区。

Can anybody advice me on the way to go here? 有人可以建议我去这里吗?

edit: The author of seek-bzip kindly created a wrapper to turn his synchronous streams into asynchronous ones, but this fix depends on node-fibers which again uses node-gyp which in my case is undesirable. 编辑: seek-bzip的作者友好地创建了一个包装器,以将其同步流转换为异步流,但是此修复方法取决于node-fibers ,该node-fibers再次使用node-gyp ,在我看来,这是不可取的。 See https://github.com/cscott/seek-bzip/issues/1 参见https://github.com/cscott/seek-bzip/issues/1

edit2: I'm still looking for a cross-platform solution, but here is a quick way to do this using CLI commands: edit2:我仍在寻找一种跨平台的解决方案,但这是使用CLI命令的一种快速方法:

var cmd = 'bunzip2 -c ' + sourceFile + ' | (cd ' + targetDir + '; tar -xf -)';

require('child_process').exec(cmd, function (err, stdout, stderr) {
    if (err) {
        // bad
    }
    // yea!
});

I feel like this question is really 2 questions: how to decrypt bz2 and how to untar. 我觉得这个问题实际上是两个问题:如何解密bz2和如何解压缩。 I'll answer the untaring part. 我将回答解皮部分。 The tar-stream module is a pretty good one: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM