How do I import an Eclipse project from a .zip fil

2020-06-25 04:40发布

问题:

I'm trying to figure out how to import an Eclipse project from an archive file (a .zip) programmatically - I want to do the same thing the import wizard does, but automatically (re-importing the same project regularly using the wizard is starting to feel really long-winded). I've found some related questions (e.g. Programmatically importing an existing project into Eclipse), but I can't figure out how to get the same sort of thing working for the .zip import.

My current thinking is as follows: if I can get a project description from the .zip somehow, then I can programmatically create the project (as per the referenced question). From there, I'm hoping I can:

  • Create a ZipLeveledStructureProvider (http://javasourcecode.org/html/open-source/eclipse/eclipse-3.5.2/org/eclipse/ui/internal/wizards/datatransfer/ZipLeveledStructureProvider.java.html) for the .zip file.
  • Run an ImportOperation (http://javasourcecode.org/html/open-source/eclipse/eclipse-3.5.2/org/eclipse/ui/wizards/datatransfer/ImportOperation.java.html) to import the contents of the .zip into the created project.

Does this make any sense? (If not, what should I be doing please?) If so, how should I be going about getting a project description from the .zip?

回答1:

For what it's worth, this seems to work (pre-tidying):

IWorkspace workspace = this.project.getWorkspace();
IProjectDescription newProjectDescription = workspace.newProjectDescription(projectName);
IProject newProject = workspace.getRoot().getProject(projectName);
newProject.create(newProjectDescription, null);
newProject.open(null);

zipFile = new ZipFile(workspace.getRoot().getLocation() + "/" + projectName + ".zip");
IOverwriteQuery overwriteQuery = new IOverwriteQuery() {
    public String queryOverwrite(String file) { return ALL; }
};
ZipLeveledStructureProvider provider = new ZipLeveledStructureProvider(zipFile);
List<Object> fileSystemObjects = new ArrayList<Object>();
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
    fileSystemObjects.add((Object)entries.nextElement());
}
ImportOperation importOperation = new ImportOperation(newProject.getFullPath(), new ZipEntry(projectName), provider, overwriteQuery, fileSystemObjects);
importOperation.setCreateContainerStructure(false);
importOperation.run(new NullProgressMonitor());