Wednesday, 24 May 2017

How to untar a file - Java Program

This post shows how to untar a tar file. It has both the steps to first uncompress a .tar.gz file and later untar it.

Using Apache Commons Compress

Apache Commons Compress library is used in the code for untarring a file. You can download it from here –

Make sure to add commons-compress-xxx.jar in your application’s class path. I have used commons-compress-1.13 version.

Example Code

This Java program has two methods deCompressGZipFile() method is used to uncompress a .tar.gz file to get a .tar file. Using unTarFile() method this .tar file is untarred.


import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;

public class UnTarDemo {

    public static void main(String[] args) {
        // Path to input file, which is a 
        // tar file compressed to create gzip file
        String INPUT_FILE = "G:\\Test.tar.gz";
        // This folder should exist, that's where
        // .tar file will go
        String TAR_FOLDER = "G:\\TarFile";
        // After untar files will go to this folder
        String DESTINATION_FOLDER = "G:\\Temp";
        UnTarDemo unTarDemo = new UnTarDemo();
        try {
            File inputFile = new File(INPUT_FILE);
            String outputFile = getFileName(inputFile, TAR_FOLDER);
            System.out.println("outputFile " + outputFile);
            File tarFile = new File(outputFile);
            // Calling method to decompress file
            tarFile = unTarDemo.deCompressGZipFile(inputFile, tarFile);
            File destFile = new File(DESTINATION_FOLDER);
            // Calling method to untar file
            unTarDemo.unTarFile(tarFile, destFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
     * @param tarFile
     * @param destFile
     * @throws IOException
    private void unTarFile(File tarFile, File destFile) throws IOException{
        FileInputStream fis = new FileInputStream(tarFile);
        TarArchiveInputStream tis = new TarArchiveInputStream(fis);
        TarArchiveEntry tarEntry = null;
        // tarIn is a TarArchiveInputStream
        while ((tarEntry = tis.getNextTarEntry()) != null) {
            File outputFile = new File(destFile + File.separator + tarEntry.getName());
                System.out.println("outputFile Directory ---- " 
                    + outputFile.getAbsolutePath());
                //File outputFile = new File(destFile + File.separator + tarEntry.getName());
                System.out.println("outputFile File ---- " + outputFile.getAbsolutePath());
                FileOutputStream fos = new FileOutputStream(outputFile); 
                IOUtils.copy(tis, fos);
     * Method to decompress a gzip file
     * @param gZippedFile
     * @param newFile
     * @throws IOException
    private File deCompressGZipFile(File gZippedFile, File tarFile) throws IOException{
        FileInputStream fis = new FileInputStream(gZippedFile);
        GZIPInputStream gZIPInputStream = new GZIPInputStream(fis);
        FileOutputStream fos = new FileOutputStream(tarFile);
        byte[] buffer = new byte[1024];
        int len;
        while((len = > 0){
            fos.write(buffer, 0, len);
        return tarFile;
     * This method is used to get the tar file name from the gz file
     * by removing the .gz part from the input file
     * @param inputFile
     * @param outputFolder
     * @return
    private static String getFileName(File inputFile, String outputFolder){
        return outputFolder + File.separator + 
                inputFile.getName().substring(0, inputFile.getName().lastIndexOf('.'));

That's all for this topic How to untar a file - Java Program. If you have any doubt or any suggestions to make please drop a comment. Thanks!

Related Topics

  1. Creating tar file and GZipping multiple files - Java Program
  2. Zipping files in Java
  3. Unzipping files in Java
  4. Compressing and Decompressing File in GZIP Format
  5. How to read file from the last line in Java

You may also like -

>>>Go to Java Programs page

No comments:

Post a Comment