代码实现拷贝Ecplise Plugin源码工程的文件(目录)到运行时目录

    下面这段代码用来拷贝当前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。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值