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();
Array ( ) 2 Responses to “How to create a tar archive using Java”
Leave a Reply
You must be logged in to post a comment.
October 28th, 2010 at 9:37 pm
Great help. Thanks
But I had to add
tar.closeArchive();
ahead of out.close();
otherwise the tar file was empty.
November 20th, 2010 at 2:30 pm
Thanks for the feedback, have updated the example