Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试Eclipse包中的URL是否为目录?_Eclipse_Eclipse Plugin - Fatal编程技术网

如何测试Eclipse包中的URL是否为目录?

如何测试Eclipse包中的URL是否为目录?,eclipse,eclipse-plugin,Eclipse,Eclipse Plugin,我正试图从插件中内置的包的内容填充目录。下面的代码在捆绑包是文件系统时工作,但在捆绑包是JAR时失败 测试URL是否为目录的最佳方法是什么?还是有一种完全不同的、更好的方法从资源包创建文件结构 static private void bundleCopy(String dir, String destination) throws IOException { Bundle bundle = com.mds.apg.Activator.getDefault().getBundle(

我正试图从插件中内置的包的内容填充目录。下面的代码在捆绑包是文件系统时工作,但在捆绑包是JAR时失败

测试URL是否为目录的最佳方法是什么?还是有一种完全不同的、更好的方法从资源包创建文件结构

    static private void bundleCopy(String dir, String destination) throws IOException {
    Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();

    for (@SuppressWarnings("unchecked")
        Enumeration<URL> en = (Enumeration<URL>) bundle.findEntries(dir, "*", true); 
            en.hasMoreElements();) {
        URL url = en.nextElement();
        String toFileName = destination + url.getPath().substring(dir.length());
        File toFile = new File(toFileName);
        InputStream in;

        try {
            in = url.openStream();
        } catch (FileNotFoundException e) {
            // this exception get thrown for file system directories but not for jar file ones
            if (!toFile.mkdir()) {
                throw new IOException("bundleCopy: " + "directory Creation Failed: "
                        + toFileName);
            }
            continue;
        }
        FileCopy.coreStreamCopy(in, toFile);
    }
}
static private void bundleCopy(String dir,String destination)抛出IOException{
Bundle Bundle=com.mds.apg.Activator.getDefault().getBundle();
对于(@SuppressWarnings(“未选中”)
枚举en=(枚举)bundle.findEntries(dir,“*”,true);
en.hasMoreElements();){
URL=en.nextElement();
字符串toFileName=destination+url.getPath().substring(dir.length());
File toFile=新文件(toFileName);
输入流输入;
试一试{
in=url.openStream();
}catch(filenotfounde异常){
//对于文件系统目录,但对于jar文件目录,会引发此异常
如果(!toFile.mkdir()){
抛出新IOException(“bundleCopy:+”目录创建失败:”
+托菲尔名称);
}
继续;
}
FileCopy.coreStreamCopy(in,toFile);
}
}

您可以尝试类似于
新建文件(FileLocator.resolve(url.toUri())
的方法,将Eclipse特定url转换为使用本机Java协议的url。

我找到了一个答案:

关键是目录的枚举项以“/”结尾

以下内容正确区分了JAR和文件系统的目录和文件:

    static private void bundleCopy(String dir, String destination) 
    throws IOException, URISyntaxException {

    Bundle bundle = com.mds.apg.Activator.getDefault().getBundle();
    Enumeration<URL> en = bundle.findEntries(dir, "*", true);
    while (en.hasMoreElements()) {
        URL url = en.nextElement();
        String pathFromBase = url.getPath().substring(dir.length()+1);
        String toFileName = destination + pathFromBase;
        File toFile = new File(toFileName);

        if (pathFromBase.lastIndexOf('/') == pathFromBase.length() - 1) {
            // This is a directory - create it and recurse
            if (!toFile.mkdir()) {
                throw new IOException("bundleCopy: " + "directory Creation Failed: " + toFileName);
            }
        } else {
            // This is a file - copy it
            FileCopy.coreStreamCopy(url.openStream(), toFile);
        }        
    }
}
static private void bundleCopy(字符串目录,字符串目标)
抛出IOException,URISyntaxException{
Bundle Bundle=com.mds.apg.Activator.getDefault().getBundle();
枚举en=bundle.findEntries(dir,“*”,true);
while(en.hasMoreElements()){
URL=en.nextElement();
字符串pathFromBase=url.getPath().substring(dir.length()+1);
字符串toFileName=destination+pathFromBase;
File toFile=新文件(toFileName);
if(pathFromBase.lastIndexOf('/')==pathFromBase.length()-1){
//这是一个目录-创建它并递归
如果(!toFile.mkdir()){
抛出新IOException(“bundleCopy:+”目录创建失败:“+toFileName”);
}
}否则{
//这是一个文件-复制它
coreStreamCopy(url.openStream(),toFile);
}        
}
}

谢谢Fabian,但是当bundle是一个jar时,toURI()调用会产生一个弹出错误:URI不是层次结构的。如果捆绑包是一个文件系统,它也不能完全工作,因为会为包含空格的文件生成异常。