Search in sources :

Example 6 with TarFile

use of org.eclipse.ui.internal.wizards.datatransfer.TarFile in project knime-core by knime.

the class WorkflowImportSelectionPage method collectWorkflowsFromZipFile.

private void collectWorkflowsFromZipFile(final String path) {
    ILeveledImportStructureProvider provider = null;
    if (ArchiveFileManipulations.isTarFile(path)) {
        try {
            TarFile sourceTarFile = new TarFile(path);
            provider = new TarLeveledStructureProvider(sourceTarFile);
        } catch (Exception io) {
            // no file -> list stays empty
            setErrorMessage("Invalid .tar file: " + path + ". Contains no workflow.");
        }
    } else if (ArchiveFileManipulations.isZipFile(path)) {
        try {
            ZipFile sourceFile = new ZipFile(path);
            provider = new ZipLeveledStructureProvider(sourceFile);
        } catch (Exception e) {
            // no file -> list stays empty
            setErrorMessage("Invalid .zip file: " + path + ". Contains no workflows");
        }
    }
    // TODO: store only the workflows (dirs are created automatically)
    final ILeveledImportStructureProvider finalProvider = provider;
    if (provider != null) {
        // reset error
        setErrorMessage(null);
        try {
            getContainer().run(true, true, new IRunnableWithProgress() {

                @Override
                public void run(final IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    Object child = finalProvider.getRoot();
                    m_importRoot = new WorkflowImportElementFromArchive(finalProvider, child, 0);
                    monitor.beginTask("Scanning for workflows in ", IProgressMonitor.UNKNOWN);
                    collectWorkflowsFromProvider((WorkflowImportElementFromArchive) m_importRoot, monitor);
                }
            });
        } catch (Exception e) {
            String message = "Error while trying to import workflows from " + path;
            IStatus status = new Status(IStatus.ERROR, KNIMEUIPlugin.PLUGIN_ID, message, e);
            setErrorMessage(message);
            LOGGER.error(message, e);
            ErrorDialog.openError(getShell(), "Import Error", null, status);
        }
        validateWorkflows();
        m_workflowListUI.setInput(m_importRoot);
        m_workflowListUI.expandAll();
        m_workflowListUI.setAllChecked(true);
        m_workflowListUI.refresh(true);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ZipLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TarLeveledStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.TarLeveledStructureProvider) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) ILeveledImportStructureProvider(org.eclipse.ui.internal.wizards.datatransfer.ILeveledImportStructureProvider)

Example 7 with TarFile

use of org.eclipse.ui.internal.wizards.datatransfer.TarFile 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)

Example 8 with TarFile

use of org.eclipse.ui.internal.wizards.datatransfer.TarFile in project tmdm-studio-se by Talend.

the class MDMImportItemUtil method upzipFile.

@SuppressWarnings("restriction")
private static File upzipFile(TarFile tarFile) {
    String targetFolder = IOUtil.getTempFolder().getAbsolutePath();
    try {
        IOUtilExt.extractTarFile(tarFile.getName(), targetFolder);
        File file = new File(targetFolder);
        return file.listFiles()[0];
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) ZipToFile(org.talend.mdm.commmon.util.workbench.ZipToFile) File(java.io.File)

Example 9 with TarFile

use of org.eclipse.ui.internal.wizards.datatransfer.TarFile in project tmdm-studio-se by Talend.

the class MDMImportItemUtil method buildUnzippedTempFile.

public static File buildUnzippedTempFile(Object fileorDirectory) {
    File tempFile = null;
    try {
        if (fileorDirectory instanceof File) {
            String targetFolder = IOUtil.getTempFolder().getAbsolutePath();
            FilesUtils.copyDirectory((File) fileorDirectory, new File(targetFolder));
            tempFile = new File(new File(targetFolder), ((File) fileorDirectory).getName());
        } else if (fileorDirectory instanceof ZipFile) {
            tempFile = upZipFile((ZipFile) fileorDirectory);
        } else if (fileorDirectory instanceof TarFile) {
            tempFile = upzipFile((TarFile) fileorDirectory);
        }
        return tempFile;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    return null;
}
Also used : ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) ZipToFile(org.talend.mdm.commmon.util.workbench.ZipToFile) File(java.io.File)

Example 10 with TarFile

use of org.eclipse.ui.internal.wizards.datatransfer.TarFile in project tmdm-studio-se by Talend.

the class MDMImportItemUtilTest method createTarFile.

private void createTarFile(String file) {
    TarOutputStream tos = null;
    try {
        File tarfile = new File(file);
        if (!tarfile.exists()) {
            tarfile.createNewFile();
        }
        tos = new TarOutputStream(new FileOutputStream(tarfile));
        // $NON-NLS-1$
        TarEntry te = new TarEntry("test.txt");
        // $NON-NLS-1$ //$NON-NLS-2$
        byte[] data = "some-content".getBytes("UTF-8");
        te.setSize(data.length);
        tos.putNextEntry(te);
        tos.write(data);
        tos.closeEntry();
        tos.flush();
        tos.finish();
    } catch (Exception e) {
        // 
        log.error(e.getMessage(), e);
    } finally {
        if (tos != null) {
            try {
                tos.close();
            } catch (IOException e) {
                // 
                log.error(e.getMessage(), e);
            }
        }
    }
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) FileOutputStream(java.io.FileOutputStream) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) TarFile(org.eclipse.ui.internal.wizards.datatransfer.TarFile) IOException(java.io.IOException)

Aggregations

TarFile (org.eclipse.ui.internal.wizards.datatransfer.TarFile)11 ZipFile (java.util.zip.ZipFile)10 File (java.io.File)9 TarLeveledStructureProvider (org.eclipse.ui.internal.wizards.datatransfer.TarLeveledStructureProvider)6 ZipLeveledStructureProvider (org.eclipse.ui.internal.wizards.datatransfer.ZipLeveledStructureProvider)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Iterator (java.util.Iterator)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)3 FileOutputStream (java.io.FileOutputStream)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 TarException (org.eclipse.ui.internal.wizards.datatransfer.TarException)2 ZipToFile (org.talend.mdm.commmon.util.workbench.ZipToFile)2 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1