如何在Java中提取tar文件? [英] How do I extract a tar file in Java?
问题描述
如何在Java中提取tar(或tar.gz或tar.bz2)文件?
How do I extract a tar (or tar.gz, or tar.bz2) file in Java?
推荐答案
注意:此功能后来通过一个单独的项目Apache Commons Compress发布,如在另一个答案中所述。这个答案是过时的。
Note: This functionality was later published through a separate project, Apache Commons Compress, as described in another answer. This answer is out of date.
我没有直接使用tar API,但tar和bzip2是在Ant中实现;你可以借用它们的实现,或者可以使用Ant来做你需要的。
I haven't used a tar API directly, but tar and bzip2 are implemented in Ant; you could borrow their implementation, or possibly use Ant to do what you need.
Gzip是Java SE的一部分(我猜测Ant实现遵循相同的模型)。
Gzip is part of Java SE (and I'm guessing the Ant implementation follows the same model).
GZIPInputStream
只是一个 InputStream
装饰器。例如,您可以在 GZIPInputStream
中包装 FileInputStream
,并以与使用任何方式相同的方式使用它 InputStream
:
GZIPInputStream
is just an InputStream
decorator. You can wrap, for example, a FileInputStream
in a GZIPInputStream
and use it in the same way you'd use any InputStream
:
InputStream is = new GZIPInputStream(new FileInputStream(file));
(注意GZIPInputStream有自己的内部缓冲区,所以包装
可能会降低性能。) BufferedInputStream
中的FileInputStream
(Note that the GZIPInputStream has its own, internal buffer, so wrapping the FileInputStream
in a BufferedInputStream
would probably decrease performance.)
这篇关于如何在Java中提取tar文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!