Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Make TAR archive

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Make TAR archive

    Hi. I am trying to make a program that creates Tar archives. Can someone help me out with this?

    First, I was using an API called "Apache Commons Compress". The only problem with this, is that it doesn't allow you to add more than 2GB into the archive. Is there some other API I can use that can make archives with more than 2GB of data?

    Also, I'm having trouble having the archive keep a directory structure. I'd like the user to point to an entire directory on the system, and then add whatever that directory contains, files, folders, and all, to the Tar. Any help with this would be MUCH appreciated!

    EDIT: Sorry, I forgot to include the link to Apache Commons Compress. Here it is, if it is any use to someone.
    Last edited by Kanyon; September 17th, 2011 at 06:40 PM. Reason: Forgot link


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Make TAR archive

    Try compressing the directory structure and files using the Zip utilities provided by the java standard edition. See
    java.util.zip (Java Platform SE 6)
    or google to find examples of how to use this library.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Make TAR archive

    I'm not looking for how to make Zip archives, I'm trying to make Tar archives.

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Make TAR archive

    Is that a limitation of the code you're using, or of the underlying fs / OS? Try searching for "tar archive 2GB limit", and you should see that at least you're not experiencing pain on your own.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Make TAR archive

    Quote Originally Posted by Kanyon View Post
    I'm not looking for how to make Zip archives, I'm trying to make Tar archives.
    Perhaps you misunderstood. Try to zip the file first, then tar the resulting zip.

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    19
    My Mood
    Fine
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Make TAR archive

    I don't think you understand. I want to make Tar archives straight from the system data.

    Anyway, I decided to just deal with the 2GB file size limit. If anyone wants it, here is my code:

    	public static void createTarOfDirectory(String directoryPath, String tarPath) throws IOException {
    		FileOutputStream fOut = null;
    		BufferedOutputStream bOut = null;
    		TarArchiveOutputStream tOut = null;
     
    		try {
    		fOut = new FileOutputStream(new File(tarPath));
    		bOut = new BufferedOutputStream(fOut);
    		tOut = new TarArchiveOutputStream(bOut);
     
    		addFileToTar(tOut, directoryPath, "");
    		} finally {
    		tOut.finish();
     
    		tOut.close();
    		bOut.close();
    		fOut.close();
    		}
    		}
     
    		 * Creates a tar entry for the path specified with a name built from the base passed in and the file/directory
    		private static void addFileToTar(TarArchiveOutputStream tOut, String path, String base) throws IOException {
    		File f = new File(path);
    		String entryName = base + f.getName();
    		TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
     
    		tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
    		tOut.putArchiveEntry(tarEntry);
     
    		if (f.isFile()) {
    		IOUtils.copy(new FileInputStream(f), tOut);
     
    		tOut.closeArchiveEntry();
    		} else {
    		tOut.closeArchiveEntry();
     
    		File[] children = f.listFiles();
     
    		if (children != null) {
    		for (File child : children) {
    		addFileToTar(tOut, child.getAbsolutePath(), entryName + "/");
    		}
    		}
    		}
    		}

    This uses the Apache Commons Compress library.

Similar Threads

  1. How can I make this shorter
    By mike2452 in forum The Cafe
    Replies: 2
    Last Post: August 7th, 2011, 10:58 PM
  2. [SOLVED] Ok, I'm trying to make my own Map classes.
    By javapenguin in forum Collections and Generics
    Replies: 3
    Last Post: August 4th, 2011, 09:13 PM
  3. trying to make game
    By knoxy5467 in forum Java Theory & Questions
    Replies: 3
    Last Post: June 20th, 2011, 09:26 AM
  4. Trying to make a bot
    By ighor10 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 11th, 2010, 02:07 PM
  5. How to make it???
    By Subhasis Banerjee in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: October 29th, 2009, 02:49 PM

Tags for this Thread