@Override public void run(IProgressMonitor monitor) throws InvocationTargetException, OperationCanceledException { monitor.beginTask( NLS.bind( DataTransferMessages.SmartImportWizardPage_expandingArchive, archive.getName(), destination.getName()), 1); TarFile tarFile = null; ZipFile zipFile = null; ILeveledImportStructureProvider importStructureProvider = null; try { if (ArchiveFileManipulations.isTarFile(archive.getAbsolutePath())) { tarFile = new TarFile(archive); importStructureProvider = new TarLeveledStructureProvider(tarFile); } else if (ArchiveFileManipulations.isZipFile(archive.getAbsolutePath())) { zipFile = new ZipFile(archive); importStructureProvider = new ZipLeveledStructureProvider(zipFile); } LinkedList<Object> toProcess = new LinkedList<>(); toProcess.add(importStructureProvider.getRoot()); while (!toProcess.isEmpty()) { if (monitor.isCanceled()) { throw new OperationCanceledException(); } Object current = toProcess.pop(); String path = importStructureProvider.getFullPath(current); File toCreate = null; if (path.equals("/")) { // $NON-NLS-1$ toCreate = destination; } else { toCreate = new File(destination, path); } if (importStructureProvider.isFolder(current)) { toCreate.mkdirs(); } else { try (InputStream content = importStructureProvider.getContents(current)) { // known IImportStructureProviders already log an // exception before returning null if (content != null) { Files.copy(content, toCreate.toPath()); } } } List<?> children = importStructureProvider.getChildren(current); if (children != null) { toProcess.addAll(children); } } monitor.worked(1); monitor.done(); } catch (Exception ex) { throw new InvocationTargetException(ex); } finally { if (importStructureProvider != null) { importStructureProvider.closeArchive(); } if (tarFile != null) try { tarFile.close(); } catch (IOException ex) { } if (zipFile != null) try { zipFile.close(); } catch (IOException ex) { } } }
static boolean isValidArchive(File file) { return ArchiveFileManipulations.isZipFile(file.getAbsolutePath()) || ArchiveFileManipulations.isTarFile(file.getAbsolutePath()); }