Search in sources :

Example 1 with TarEntry

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;
        }
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) TarEntry(org.eclipse.ui.internal.wizards.datatransfer.TarEntry) IOException(java.io.IOException) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) File(java.io.File) TarException(org.eclipse.ui.internal.wizards.datatransfer.TarException) IOException(java.io.IOException)

Aggregations

File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 TarEntry (org.eclipse.ui.internal.wizards.datatransfer.TarEntry)1 TarException (org.eclipse.ui.internal.wizards.datatransfer.TarException)1 TarFile (org.eclipse.ui.internal.wizards.datatransfer.TarFile)1