import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
public void untar(String tarlocation, String tarname, String untarlocation){
//method variables
private File tarFile;
private TarArchiveInputStream tar_is;
private FileInputStream fin;
private TarArchiveEntry entry;
private File entryDestination;
private OutputStream out;
tarFile = new File(tarlocation+tarname);
fin = new FileInputStream(tarFile);
tar_is = new TarArchiveInputStream(fin);
//untar
//for each reference, untar to untarlocation, creating directories if needed
while ((entry = tar_is.getNextTarEntry()) != null) {
entryDestination = new File(untarlocation, entry.getName());
//if necessary create dir structure
entryDestination.getParentFile().mkdirs();
if (entry.isDirectory())
entryDestination.mkdirs();
else {
try{
//untar current entry
out = new FileOutputStream(entryDestination);
IOUtils.copy(tar_is, out);
}
catch(Exception e){
throw e;
}
finally{
//close streams ignoring exceptions
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(fin);
IOUtils.closeQuietly(tar_is);
}
}
}
}
03/08/2015
[Java] untar
Now that we know how to tar in Java, let's see how to unzip using the same Apache Commons Compress library:
Tag:
HowTo,
Java,
Source code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
With great power comes great responsibility