How to programmatically copy a folder from a plug-in to a new project in the workspace?

I am developing an Eclipse plugin by creating a new project wizard. When creating such a new project in the workspace, I need to copy the folder and its descendant from the plug-in to the newly created project in the workspace. The problem is that although the project is an IResource , the plugin folder is on the file system.

I managed to get the URL for the source plugin folder that I need to copy, and I have a link to IProject.

What I need to know: how to copy the first to the last?

+4
source share
2 answers

Check out this answer to find out how to get the file / folder "from" the plugin.

Then create new files / folders in the projects and set the contents of the file 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); } } } 
+5
source

This is impossible without knowing the exact files (you cannot iterate over children). Instead of using a folder with files and subfolders, create a zip with this structure and unzip the zip in the workspace (this should save the desired structure).

0
source

Source: https://habr.com/ru/post/1390084/


All Articles