Jul 13

Java doesn’t provide standard support for using tar archives so a 3rd party implementation is required. One such implementation is provided by ICE Engineering and can be found here.

Here is a simple example of its usage to tar the files in a directory.

		File tarFile = new File("my.tar");
		FileOutputStream out = new FileOutputStream(tarFile);
		TarArchive tar = new TarArchive(out);
		File[] files = directory.listFiles();
		for (File file : files) {
			System.out.println("Adding "+file);
			TarEntry tarEntry = new TarEntry(file);
			tar.writeEntry(tarEntry, false);
		}
                tar.closeArchive();
		out.close();

written by objects \\ tags: , , , ,


2 Responses to “How to create a tar archive using Java”

  1. Andi Says:

    Great help. Thanks
    But I had to add
    tar.closeArchive();
    ahead of out.close();
    otherwise the tar file was empty.

  2. objects Says:

    Thanks for the feedback, have updated the example

Leave a Reply

You must be logged in to post a comment.