// Package

// Imports
import java.io.*;
import com.ice.tar.*;
import java.util.zip.GZIPInputStream;

/**
 * (Name of class).
 * <p>
 * (Short description)
 * <p>
 * For Mr. Benum, 
 * Comp Info Sci, ICS4M2
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Philip Peng
 * @version	X.X Dec 20, 2007
 */

public class TarGzTest {

	// Variables

	// Methods

	public static void main(String[] args) {
		try {
			String file = "podzilla2.tar.gz";
			String drive = "C:\\";
			String dest = drive + "foo\\";
			untargz(file, dest);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	
	private static void untargz(String file, String dest) throws IOException {
		
		// Make directories
		File destFolder = new File(dest);
		destFolder.mkdirs();
		
		// Create file
		TarInputStream f = new TarInputStream ( new GZIPInputStream ( new FileInputStream( new File (file) ) ) );
		
		// Extract
		TarEntry tarFile;
		while ( ( tarFile = f.getNextEntry() ) != null ) {
			// Extraction file path
			File destFile = new File ( destFolder.getPath() + File.separatorChar  + tarFile.getName() );
			if (tarFile.isDirectory()) { // Directory
				// Make directory
				destFile.mkdirs();
			} else { // File
				// Extract to location
				FileOutputStream out = new FileOutputStream(destFile);
				f.copyEntryContents(out);
				out.close();
			}
		}
		f.close();

	}
	
}
