Extracting tar file using JAVA
At last i found a library to untar / tar files using JAVA. Here i provide an example how to use the library. Some of the codes have been taken from other users in forums to enhance the code.
import com.ice.tar.TarInputStream;
import java.io.File;
import java.util.zip.GZIPInputStream;
import com.ice.tar.TarEntry;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.*;
//————–
public void execute() {
TarInputStream tin = null;
try {
tin = new TarInputStream(new GZIPInputStream(new
FileInputStream(new File(tf.getTarFileName()))));
TarEntry tarEntry = tin.getNextEntry();
while (tarEntry != null) {
File destPath = new File( +
File.separatorChar +
tarEntry.getName());
if (!tarEntry.isDirectory()) {
// start of new code
String pathStr = destPath.getPath();
int idx = pathStr.lastIndexOf(File.separatorChar);
if (idx > 0) {
File destDir = new File(pathStr.substring(0, idx));
destDir.mkdirs();
}
// end of new code
FileOutputStream fout = new FileOutputStream(destPath);
tin.copyEntryContents(fout);
fout.close();
}
else {
destPath.mkdir();
}
tarEntry = tin.getNextEntry();
}
tin.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}

Leave a Reply