extracting .tar files
807588Nov 23 2004 — edited Jan 28 2009I have the com.ice.tar package from http://www.trustice.com/java/tar/ and I have the code shown below compiling just fine. However, I need to extract .tar files and this code will only extract tar.gz files. Does anyone know what I need to change to my code to get it to extract .tar files? Thanks,
import com.ice.tar.*;
import javax.activation.*;
import java.io.*;
import java.util.zip.GZIPInputStream;
public class untarFiles
{
public static void main(String args[]) {
try {
untar("c:/Files/AnyFile.tar.gz",new File("c:/Files/"));
}
catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
private static void untar(String tarFileName, File dest) throws IOException {
//assuming the file you pass in is not a dir
dest.mkdir();
//create tar input stream from a .tar.gz file
TarInputStream tin = new TarInputStream( new GZIPInputStream
( new FileInputStream(new File(tarFileName))));
//get the first entry in the archive
TarEntry tarEntry = tin.getNextEntry();
while (tarEntry != null){//create a file with the same name as the tarEntry
File destPath = new File(dest.toString() + File.separatorChar + tarEntry.getName());
if(tarEntry.isDirectory()){
destPath.mkdir();
} else {
FileOutputStream fout = new FileOutputStream(destPath);
tin.copyEntryContents(fout);
fout.close();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}
}