How to copy a resource directory and all included files and sub-folders in a Jar onto another directory

丶灬走出姿态 提交于 2019-12-24 01:44:29

问题


Is it possible to copy a resource folder, and all the files within, including all directories and sub directories therein into another directory?

What I've managed so far is to copy only one file resource, which is a CSS file:

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();
        }

    }
}

回答1:


You need Bundle.findEntries(path,mask,recurse). This method was designed for this purpose, works beautifully with fragments as well.

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());
       }
     }
}


来源:https://stackoverflow.com/questions/36400436/how-to-copy-a-resource-directory-and-all-included-files-and-sub-folders-in-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!