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);
}
}
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;
}
}
}
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;
}
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;
}
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);
}
}
}
}
Aggregations