Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extract A tar.gz file

807589Jul 10 2008 — edited Jul 11 2008
Ok guys,

I have a program that extracts the contents of a tar.gz file. Right now it does extract one of the files and then throws an error:
com.ice.tar.InvalidHeaderException: bad header in block 9 record 10, header magi
c is not 'ustar' or unix-style zeros, it is '1141115144674854', or (dec) 114, 11
1, 51, 44, 67, 48, 54
        at com.ice.tar.TarInputStream.getNextEntry(Unknown Source)
        at Extract_TAR_GZ_FILE.untar(Extract_TAR_GZ_FILE.java:37)
        at Extract_TAR_GZ_FILE.run(Extract_TAR_GZ_FILE.java:55)
        at Extract_TAR_GZ_FILE.main(Extract_TAR_GZ_FILE.java:67)
bad header in block 9 record 10, header magic is not 'ustar' or unix-style zeros
, it is '1141115144674854', or (dec) 114, 111, 51, 44, 67, 48, 54 
The class that extracts the files:
import java.io.*;
import com.ice.tar.*;
import javax.activation.*;
import java.util.zip.GZIPInputStream;

public class Extract_TAR_GZ_FILE {
	
	public static InputStream getInputStream(String tarFileName) throws Exception{
    
      if(tarFileName.substring(tarFileName.lastIndexOf(".") + 1, tarFileName.lastIndexOf(".") + 3).equalsIgnoreCase("gz")){
         System.out.println("Creating an GZIPInputStream for the file");
         return new GZIPInputStream(new FileInputStream(new File(tarFileName)));
    
      }else{
         System.out.println("Creating an InputStream for the file");
         return new FileInputStream(new File(tarFileName));
      }
   }
	
	private static void untar(InputStream in, String untarDir) throws IOException {
		
	  System.out.println("Reading TarInputStream... (using classes from http://www.trustice.com/java/tar/)");
      TarInputStream tin = new TarInputStream(in);
      TarEntry tarEntry = tin.getNextEntry();
      if(new File(untarDir).exists()){
	      while (tarEntry != null){
	         File destPath = new File(untarDir + File.separatorChar + tarEntry.getName());
	         System.out.println("Processing " + destPath.getAbsoluteFile());
	         if(!tarEntry.isDirectory()){
	            FileOutputStream fout = new FileOutputStream(destPath);
	            tin.copyEntryContents(fout);
	            fout.close();
	         }else{
	            destPath.mkdir();
	         }
	         tarEntry = tin.getNextEntry();
	      }
	      tin.close();
      }else{
         System.out.println("That destination directory doesn't exist! " + untarDir);
      }
		
	}
	
	private void run(){
		
		try {			
			String strSourceFile = "G:/source/BROKERH_20080303_A2008_S0039.TAR.GZ";
			String strDest = "G:/source/Extracted Files";
			InputStream in = getInputStream(strSourceFile);
		
			untar(in, strDest);		
			
		}catch(Exception e) {
		
			e.printStackTrace();		
			System.out.println(e.getMessage());
		}
	}	
	
	public static void main(String[] strArgs) throws IOException{
		new Extract_TAR_GZ_FILE().run();
	}
}
The file I tested with can be found here:
http://www.yourfilehost.com/media.php?cat=other&file=BROKERH_20080303_A2008_S0039.TAR.GZ

Any help? Thanks a bunch.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 8 2008
Added on Jul 10 2008
5 comments
1,120 views