如何以编程方式从 .zip 文件导入 Eclipse 项目? [英] How do I import an Eclipse project from a .zip file programmatically?

查看:45
本文介绍了如何以编程方式从 .zip 文件导入 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:

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

这篇关于如何以编程方式从 .zip 文件导入 Eclipse 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆