如何以编程方式从 .zip 文件导入 Eclipse 项目?

2022-01-16 00:00:00 import eclipse-plugin eclipse

我试图弄清楚如何以编程方式从存档文件(.zip)中导入 Eclipse 项目 - 我想做与导入向导相同的事情,但自动(重新- 使用向导定期导入同一个项目开始感觉非常冗长).我发现了一些相关的问题(例如 以编程方式将现有项目导入 Eclipse),但我不知道如何为 .zip 导入提供同样的功能.

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.

我目前的想法如下:如果我能以某种方式从 .zip 获得项目描述,那么我可以以编程方式创建项目(根据引用的问题).从那里,我希望我可以:

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:

  • 创建一个 ZipLeveledStructureProvider (http://javasourcecode.org/html/open-source/eclipse/eclipse-3.5.2/org/eclipse/ui/internal/wizards/datatransfer/.zip 文件的 ZipLeveledStructureProvider.java.html).
  • 运行 ImportOperation (http://javasourcecode.org/html/open-source/eclipse/eclipse-3.5.2/org/eclipse/ui/wizards/datatransfer/ImportOperation.java.html) 将 .zip 的内容导入到创建的项目中.
  • 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.

这有意义吗?(如果不是,请问我应该怎么做?)如果是,我应该如何从 .zip 中获取项目描述?

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?

推荐答案

对于它的价值,这似乎工作(预先整理):

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());

相关文章