Create TAR File in Java - Example Program

TAR file Creation in Java - Introduction


In this example, we will explain how to create a TAR file in Java using Apache Commons Compress library. TAR - Tape Archive is one of the popular archive formats, and Commons Compress library makes it very easy to create a TAR file. We deal this with an example so that you can understand how easy it is to deal with TAR files. Let us get started.

The test files we will use to create a TAR ball is provided below:

TAR File Creation in Java - Apache Commons Compress - Input Files
TAR File Creation in Java - Apache Commons Compress - Input Files
Make sure you download a cope of Commons Compress library and have the JAR files in your classpath, to test the examples provided in this tutorial.

Create TAR File in Java - Example Program


The complete Java program that you can use to create a TAR file in Java is provided below:

import java.io.*;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
public class TarFiles {  
        public static void main(String[] args) throws Exception{
                /* Output Stream - that will hold the physical TAR file */
                OutputStream tar_output = new FileOutputStream(new File("tar_ball.tar"));
                /* Create Archive Output Stream that attaches File Output Stream / and specifies TAR as type of compression */
                ArchiveOutputStream my_tar_ball = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, tar_output);
                /* Create Archieve entry - write header information*/
                File tar_input_file= new File("test_file_1.xml");
                TarArchiveEntry tar_file = new TarArchiveEntry(tar_input_file);
                /* length of the TAR file needs to be set using setSize method */
                tar_file.setSize(tar_input_file.length());
                my_tar_ball.putArchiveEntry(tar_file);
                IOUtils.copy(new FileInputStream(tar_input_file), my_tar_ball);
                /* Close Archieve entry, write trailer information */
                my_tar_ball.closeArchiveEntry();
                /* Repeat steps for the next file that needs to be added to the TAR */
                tar_input_file= new File("test_file_2.xml");
                tar_file = new TarArchiveEntry(tar_input_file);
                tar_file.setSize(tar_input_file.length());
                my_tar_ball.putArchiveEntry(tar_file);
                IOUtils.copy(new FileInputStream(tar_input_file), my_tar_ball);
                /* Close Archieve entry, write trailer information */
                my_tar_ball.closeArchiveEntry();
                my_tar_ball.finish(); 
                /* Close output stream, our files are zipped */
                tar_output.close();
        }
}

This program creates a TAR file with the two input files we specified earlier. The output of this program is shown below:

Create TAR file in Java - Commons Compress library - Output TAR File
Create TAR file in Java - Commons Compress library - Output TAR File
If you look at the code above, you will note that the steps to create TAR file is not the replica of Zip file. We have added a few steps in the code which is explained in comments. Also, we use TarArchiveEntry class to handle TAR file creation. Commons compress library has a separate class for handling zip files, which we discussed in create zip file in Java tutorial.

That completes the tutorial to create TAR file in Java, using Apache Commons Compress library. If you have any questions on this article, you can post it in the comments section.

3 comments:

  1. Hi,
    Thanks a lot the code help me a lot in my project.

    One question my requirement says to create pax file out of group of files what changes it needs ?

    ReplyDelete
  2. @Anonymous, to create a PAX (Portable Archive Exchange) in Java, I'm not sure if you would be able to use this example and Apache Commons Compress library. I'm working on it and will post a link with an example for you soon once I find an a solution. In the meantime, if you find an approach, please share it here.

    ReplyDelete
  3. @Anonymous,

    To create a PAX archive using Java, I tried with At4J Java API, and have some raw code below for you to refine. This should do the trick.

    import org.entityfs.WritableFile;
    import org.at4j.comp.bzip2.*;
    import java.io.*;
    import java.nio.charset.Charset;
    import org.at4j.tar.builder.*;
    import org.at4j.zip.extattrs.*;
    import org.entityfs.util.*;
    import org.entityfs.entityattrs.unix.UnixEntityMode;
    import org.entityfs.el.AbsoluteLocation;
    public class test {
    public static void main(String[] args) throws Exception{

    /* Output Stream - that will hold the physical TAR file */
    OutputStream tar_output = new FileOutputStream(new File("tar_ball.pax"));
    //define settings for the tar builder
    TarBuilderSettings settings = new TarBuilderSettings().setDefaultFileEntrySettings( new TarEntrySettings(). setEntityMode(UnixEntityMode.forCode(0640)));
    //Set the strategy for Tar builder as Pax
    settings.setEntryStrategy(new PaxTarEntryStrategy(Charset.forName("utf8")));
    // Create the Tar builder
    TarStreamBuilder builder = new TarStreamBuilder(tar_output, settings);
    // Add two files
    builder.add(new NamedReadableFileAdapter(new CharSequenceReadableFile("Test file for PAX Archive in Java 1"),"Test1.txt"),AbsoluteLocation.ROOT_DIR);
    builder.add(new NamedReadableFileAdapter(new CharSequenceReadableFile("Second Test File"),"Test 2.txt"),AbsoluteLocation.ROOT_DIR);
    // Close the builder to finish the file.
    builder.close();
    }
    }

    ReplyDelete