下面这段代码用来拷贝当前Eclipse Plugin源码工程某一目录下的所有文件(或目录)到Runtime工程Workspace指定的org.eclipse.core.resources.IFolder中
/**
* Copy the files from srcPath to destination folder. For example: you have a eclipse Plugin project
* "org.xue.project", and you want copy the files(contain directory) "org.xue.project.core/content" to runtime
* folder, will use the followings code: copyFilesToFolder("/content", true, desFolder);
*
* @param srcPath The path name in which to look. The path is always relative to the root of this bundle and may
* begin with "/". A path value of "/" indicates the root of this bundle.
* @param srcPath
* @param recurse If <code>true</code>, recurse into subdirectories(contains directories). Otherwise only return
* entries from the specified path.
* @param desFolder
* @throws IOException
* @throws CoreException
*/
@SuppressWarnings("unchecked")
private void copyFilesToFolder(String srcPath, boolean recurse, IFolder desFolder) throws IOException, CoreException {
Enumeration paths = null;
paths = CorePlugin.getDefault().getBundle().getEntryPaths(srcPath);
while (paths.hasMoreElements()) {
String nextElement = (String) paths.nextElement();
String currentPath = "/" + nextElement;
URL resourceURL = CorePlugin.getDefault().getBundle().getEntry(currentPath);
URL fileURL = null;
File file = null;
try {
fileURL = FileLocator.toFileURL(resourceURL);
file = new File(fileURL.getFile());
} catch (IOException e) {
e.printStackTrace();
}
if (file.isDirectory() && recurse) {
if (file.getName().startsWith(".")) {
continue;
}
IFolder folder = desFolder.getFolder(file.getName());
if (!folder.exists()) {
folder.create(false, true, null);
}
copyFilesToFolder(currentPath, recurse, folder);
continue;
}
String fileName = new Path(fileURL.getPath()).lastSegment();
InputStream openStream = null;
openStream = fileURL.openStream();
copyFileToFolder(openStream, fileName, desFolder);
}
}
private void copyFileToFolder(InputStream inputStream, String fileName, IFolder folder) throws CoreException {
if (inputStream == null) {
return;
}
IFile file = folder.getFile(fileName);
if (file.exists()) {
return;
}
file.create(inputStream, false, null);
}
其中CorePlugin即是当前插件工程的Activator类,继承自AbstractUIPlugin。