skiphoppy skiphoppy - 27 days ago 7
Java Question

How do I extract a tar file in Java?

How do I extract a tar (or tar.gz, or tar.bz2) file in Java?

Answer Source

Note: This functionality was later published through a separate project, Apache Commons Compress, as described in another answer. This answer is out of date.


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 is part of Java SE (and I'm guessing the Ant implementation follows the same model).

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));

(Note that the GZIPInputStream has its own, internal buffer, so wrapping the FileInputStream in a BufferedInputStream would probably decrease performance.)