How to programmatically copy a folder from a plugin to a new project in the work space?

隐身守侯 提交于 2019-12-01 04:04:10

问题


I am developing an Eclipse Plugin creating a new Project Wizard. When creating such new project in the workspace I need it to copy a folder, and its descendent, from the plugin to the just created project in the workspace. The problem is that while the project is an IResource the plugin folder is in the file system.

I succeeded in getting an URL for the source plugin folder I need to copy and I have the IProject reference.

What I need to know is: How to copy the former into the latter?


回答1:


Check out this answer to see how to get a file/folder "out of" a plugin.

Then create new files/folders in the projects and set file contents using InputStream:

void copyFiles (File srcFolder, IContainer destFolder) {
    for (File f: srcFolder.listFiles()) {
        if (f.isDirectory()) {
            IFolder newFolder = destFolder.getFolder(new Path(f.getName()));
            newFolder.create(true, true, null);
            copyFiles(f, newFolder);
        } else {
            IFile newFile = destFolder.getFile(new Path(f.getName()));
            newFile.create(new FileInputStream(f), true, null);
        }
    }
}



回答2:


This is not possible without knowing exactly the files (you cannot iterate over the children). Instead of using a folder with files and subfolders, create a zip with that structure and unpack the zip in your workspace (this should preserve the desired structure).



来源:https://stackoverflow.com/questions/8792260/how-to-programmatically-copy-a-folder-from-a-plugin-to-a-new-project-in-the-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!