5

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
Andrea Sindico
  • 7,358
  • 6
  • 47
  • 84

2 Answers2

5

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);
        }
    }
}
Community
  • 1
  • 1
Martti Käärik
  • 3,601
  • 18
  • 28
  • Yeah I did managet to get a folder out of a plugin but I actually need to copy a folder and subfolders not just a file. I guess by InputStream I only can copy files content not the whole folder (and related contents). Any advice? – Andrea Sindico Jan 09 '12 at 21:02
  • @Silli A little recursion will help out. – Martti Käärik Jan 09 '12 at 22:26
  • Does the bundle necessary need to be unpacked that I can be able to read files from it? – Danny Lo Oct 07 '14 at 10:50
  • @DannyLo Well, I'm sure it's possible to read from the bundle directly, but if you're using Eclipse resource API then it unpacks the requested entries automatically. – Martti Käärik Oct 07 '14 at 12:50
  • @MarttiKäärik I am asking because I'm [having problems with empty folders](http://stackoverflow.com/questions/26235880/filelocators-method-tofileurl-ignores-empty-folders). – Danny Lo Oct 07 '14 at 12:53
  • What if a directory with the same name already exists in the target? Then this code just fails, right? Or is anything overwritten? – Lii Jun 20 '16 at 09:28
  • Also, it seems like it will fail if parent directories to `destFolder` does not exist. In that case it seems like you must use the technique described in [this answer](http://stackoverflow.com/a/8606975/452775). – Lii Jun 20 '16 at 10:00
0

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).

Tom Seidel
  • 9,525
  • 1
  • 26
  • 38
  • this is a good advice thank you. How do I unpack programmatically? – Andrea Sindico Jan 10 '12 at 08:09
  • A good starting point is the snippet at http://stackoverflow.com/a/1303771/173101 or use the documentation at http://java.sun.com/developer/technicalArticles/Programming/compression/ – Tom Seidel Jan 10 '12 at 08:18
  • @Silli Eclipse `FileLocator.toFileURL()` will unpack the files for you **if** necessary. – Martti Käärik Jan 10 '12 at 13:24
  • @TomSeidel You *can* iterate over the children of a specific path using org.osgi.framework.Bundle#getEntryPaths(String). With proper recursion you can copy entire trees from the plug-in into the workspace, reading the returned URL's input streams to create the files and folders. – nitind Jan 07 '14 at 08:12