【问题标题】:How to copy a resource directory and all included files and sub-folders in a Jar onto another directory如何将 Jar 中的资源目录以及所有包含的文件和子文件夹复制到另一个目录
【发布时间】:2016-07-23 20:43:51
【问题描述】:

是否可以将资源文件夹以及其中的所有文件,包括其中的所有目录和子目录复制到另一个目录中?

到目前为止,我只复制了一个文件资源,即 CSS 文件:

public void addCSS() {
    Bundle bundle = FrameworkUtil.getBundle(this.getClass());
    Bundle[] bArray = bundle.getBundleContext().getBundles();
    Bundle cssBundle = null;
    for (Bundle b : bArray) {
        if (b.getSymbolicName().equals("mainscreen")) {
            cssBundle = b;
            break;
        }
    }
    Enumeration<URL> resources = null;
    try {
        resources = cssBundle.getResources("/resources/css/mainscreen.css");
    } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (resources != null) {
        URL myCSSURL = resources.nextElement();

        InputStream in;
        try {
            in = myCSSURL.openStream();
            File css = new File(this.baseDir() + "/ui/resources/css/mainscreen.css");
            try (FileOutputStream out = new FileOutputStream(css)) {
                IOUtils.copy(in, out);
            }
        } catch (IOException e) {
                // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

【问题讨论】:

  • 你一直在发额外的问题,这是否意味着你之前的问题已经解决了?如果是这样,请发布解决方案或接受其中一个答案。这使得 StackOverflow 成为对所有人有用的资源,而不仅仅是您。

标签: java file resources osgi bnd


【解决方案1】:

您需要Bundle.findEntries(path,mask,recurse)。这种方法就是为此目的而设计的,也可以很好地处理片段。

void getCSSResources( List<URL> out ) 
    for ( Bundle b : context.getBundles() {
       Enumeration<URL> e = b.findEntries("myapp/resources", "*.css", true);
       while (e.hasMoreElements() {
          out.add(e.nextElement());
       }
     }
}

【讨论】:

  • 谢谢。让我试试看。
  • 非常感谢。这很有帮助,尽管它看起来很明显。非常感激。效果很好。非常感谢您使用 Bnd Tools。它使 OSGi 变得更加轻松和愉快。太好了!
猜你喜欢
  • 1970-01-01
  • 2015-01-03
  • 2013-07-02
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2015-01-12
  • 2013-09-30
  • 1970-01-01
相关资源
最近更新 更多