public static IProject unpackResourceProject(IProject project, Example.Resource resource) throws Exception { if (resource.getType() == Type.ENVIRONMENT) CloudScaleProjectSupport.addProjectNature(project); ZipFile file = new ZipFile(resource.getArchive().getFile()); ZipFileStructureProvider provider = new ZipFileStructureProvider(file); IPath containerPath = project.getFullPath(); Object source = provider.getRoot(); IOverwriteQuery query = new IOverwriteQuery() { @Override public String queryOverwrite(String path) { return IOverwriteQuery.ALL; }; }; ImportOperation operation = new ImportOperation(containerPath, source, provider, query); operation.run(null); return project; }
/** * extract zip file and import files into project * * @param srcZipFile * @param destPath * @param monitor * @param query * @throws CoreException */ private static void importFilesFromZip( ZipFile srcZipFile, IPath destPath, IProgressMonitor monitor, IOverwriteQuery query ) throws CoreException { try { ZipFileStructureProvider structureProvider = new ZipFileStructureProvider( srcZipFile ); List list = prepareFileList( structureProvider, structureProvider .getRoot( ), null ); ImportOperation op = new ImportOperation( destPath, structureProvider.getRoot( ), structureProvider, query, list ); op.run( monitor ); } catch ( Exception e ) { String message = srcZipFile.getName( ) + ": " + e.getMessage( ); //$NON-NLS-1$ Logger.logException( e ); throw BirtCoreException.getException( message, e ); } }
private void assertEntryMatchesDir(ZipFileStructureProvider structureProvider, IContainer folder, ZipEntry parentEntry) throws Exception { List unexportedItems = Arrays.asList("bin"); IResource[] members = folder.members(); List children = structureProvider.getChildren(parentEntry); for (IResource member : members) { String memberName = member.getName(); if (unexportedItems.contains(memberName)) continue; boolean found = false; for (Object child : children) { ZipEntry entry = (ZipEntry) child; String entryName = getEntryName(entry); if (entryName.equals(memberName)) { found = true; if (member instanceof IFolder) if (entry.isDirectory()) assertEntryMatchesDir(structureProvider, (IContainer) member, entry); else fail("Directories don't match"); } } assertTrue(found); } }
public static IProject createExampleProject(IProject project, Example.Resource resource) { try { if (resource.getType() == Type.ENVIRONMENT) CloudScaleProjectSupport.addProjectNature(project); ZipFile file = new ZipFile(resource.getArchive().getFile()); ZipFileStructureProvider provider = new ZipFileStructureProvider(file); IPath containerPath = project.getFullPath(); Object source = provider.getRoot(); IOverwriteQuery query = new IOverwriteQuery() { @Override public String queryOverwrite(String path) { return IOverwriteQuery.ALL; }; }; ImportOperation operation = new ImportOperation(containerPath, source, provider, query); try { operation.run(null); } catch (Exception ex) { ex.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); project = null; } return project; }
public static void importFilesFromZip(ZipFile srcZipFile, IPath destPath, IProgressMonitor monitor) throws InvocationTargetException { ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(srcZipFile); try { ImportOperation op = new ImportOperation(destPath, structureProvider.getRoot(), structureProvider, new ImportOverwriteQuery()); op.run(monitor); } catch (InterruptedException e) { // should not happen } }
/** * Prepare file list from zip file * * @param structure * @param entry * @param list * @return */ private static List prepareFileList( ZipFileStructureProvider structure, ZipEntry entry, List list ) { if ( structure == null || entry == null ) return null; if ( list == null ) { list = new ArrayList( ); } // get children List son = structure.getChildren( entry ); if ( son == null ) return list; // check if directory Iterator it = son.iterator( ); while ( it.hasNext( ) ) { ZipEntry temp = (ZipEntry) it.next( ); if ( temp.isDirectory( ) ) { prepareFileList( structure, temp, list ); } else { // if it is file, add to list list.add( temp ); } } return list; }
@Test public void testCompleteSnapshot() throws Exception { String snapshotFile = snapshotManager.takeSnapshot(javaProject.getProject()); Thread.sleep(200); ZipFileStructureProvider structureProvider = new ZipFileStructureProvider(new ZipFile(snapshotFile)); ZipEntry rootEntry = structureProvider.getRoot(); List children = structureProvider.getChildren(rootEntry); ZipEntry projectRoot = getProjectEntry(children); assertEntryMatchesDir(structureProvider, javaProject.getProject(), projectRoot); }
private String internalChooseArchivePath() { ZipFile zipFile= null; try { if (fWorkspaceRadio.isSelected()) { IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(fArchiveField.getText())); if (resource != null) { IPath location= resource.getLocation(); if (location != null) { zipFile= new ZipFile(location.toOSString()); } } } else { zipFile= new ZipFile(fArchiveField.getText()); } if (zipFile == null) { return null; } ZipFileStructureProvider provider= new ZipFileStructureProvider(zipFile); ILabelProvider lp= new ZipDialogLabelProvider(provider); ZipDialogContentProvider cp= new ZipDialogContentProvider(provider); ElementTreeSelectionDialog dialog= new ElementTreeSelectionDialog(fShell, lp, cp); dialog.setAllowMultiple(false); dialog.setValidator(new ZipDialogValidator()); dialog.setTitle(PreferencesMessages.JavadocConfigurationBlock_browse_jarorzip_path_title); dialog.setMessage(PreferencesMessages.JavadocConfigurationBlock_location_in_jarorzip_message); dialog.setComparator(new ViewerComparator()); String init= fArchivePathField.getText(); if (init.length() == 0) { init= "docs/api"; //$NON-NLS-1$ } dialog.setInitialSelection(cp.findElement(new Path(init))); dialog.setInput(this); if (dialog.open() == Window.OK) { String name= provider.getFullPath(dialog.getFirstResult()); return new Path(name).removeTrailingSeparator().toString(); } } catch (IOException e) { JavaPlugin.log(e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException e1) { // ignore } } } return null; }
public ZipDialogContentProvider(ZipFileStructureProvider provider) { fProvider= provider; }
public ZipDialogLabelProvider(ZipFileStructureProvider provider) { fProvider= provider; }