Decompress And Untar Multiple Gzipped files in Java

In the post GZIP Multiple Files in Java Creating Tar Archive we have already seen that you need to create a tar archive for multiple files and then compress it using GZIP. In this post we’ll see how to decompress multiple gzipped files in Java which first requires to decompress a gz file (.tar.gz) and later untar that tar ball.

Decompress gzipped files and untar archive in Java

Java program given here to unarchive multiple files compressed as .tar.gz uses Apache Commons Compress library which can be downloaded from this path-

Version used here is commons-compress-1.18 so commons-compress-1.18.jar is added to the class path.

From Apache Commons Compress library two files are used for decompressing gzip file and to untar a tar archive.

TarArchiveEntry– Represents an entry in a Tar archive.

TarArchiveInputStream– This class has method getNextTarEntry() to read archive entries from TarArchiveEntry. While looping these archive entries you check if it is a directory or file. If it is a directory just create that directory, in case of file read the content of the file and write it to output stream.

Java program – Decompress Gzip file and untar

If you just want to untar a file that is you have to unarchive a .tar file not a .tar.gz file then you can skip creating GZIPInputStream.

That’s all for the topic Decompress And Untar Multiple Gzipped files in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

  • How to Unzip a File in Java
  • Find The Largest And Second Largest Element of an Array in Java
  • ConcurrentSkipListSet in Java
  • wait(), notify() And notifyAll() Methods in Java
  • Multiple Catch Blocks in Java Exception Handling
  • How to Create Bootable USB Drive For Installing Ubuntu
  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.