use of org.eclipse.ui.internal.wizards.datatransfer.TarEntry in project tmdm-studio-se by Talend.
the class IOUtilExt method extractTarFile.
public static void extractTarFile(String tarFileName, String targetFolder) throws Exception {
if (tarFileName == null || targetFolder == null) {
return;
}
Exception exception = null;
byte[] buf = new byte[8192];
TarFile tarFile = null;
try {
tarFile = new TarFile(tarFileName);
Enumeration<TarEntry> enumeration = tarFile.entries();
while (enumeration.hasMoreElements()) {
TarEntry entry = enumeration.nextElement();
File file = new File(targetFolder, entry.getName());
if (entry.getFileType() == TarEntry.DIRECTORY) {
if (!file.exists()) {
file.mkdir();
}
} else {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
InputStream zin = tarFile.getInputStream(entry);
OutputStream fout = new FileOutputStream(file);
// check if parent folder exists
File dir = file.getParentFile();
if (dir.isDirectory() && !dir.exists()) {
dir.mkdirs();
}
try {
while (true) {
int bytesRead = zin.read(buf);
if (bytesRead == -1) {
// end of file
break;
}
fout.write(buf, 0, bytesRead);
}
fout.flush();
} catch (Exception e) {
exception = e;
// stop looping
return;
} finally {
if (zin != null) {
try {
zin.close();
} catch (Exception e) {
// ignore
}
}
if (fout != null) {
try {
fout.close();
} catch (Exception e) {
// ignore
}
}
}
}
}
} catch (Exception e) {
exception = e;
} finally {
if (tarFile != null) {
try {
tarFile.close();
} catch (IOException e) {
// ignore
}
}
if (exception != null) {
// notify caller with exception
throw exception;
}
}
}
Aggregations