如何在 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
装饰器.例如,您可以将 FileInputStream
包装在 GZIPInputStream
中,并像使用任何 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 有自己的内部缓冲区,因此将 FileInputStream
包装在 BufferedInputStream
中可能会降低性能.)
(Note that the GZIPInputStream has its own, internal buffer, so wrapping the FileInputStream
in a BufferedInputStream
would probably decrease performance.)
这篇关于如何在 Java 中提取 tar 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!