2

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

    }
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142
  • You keep posting additional questions, does this mean your previous problems have been solved? If so please post the solution or accept one of the answers. This makes StackOverflow a useful resource for everybody, not just you. – Neil Bartlett Apr 04 '16 at 21:46

1 Answers1

3

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());
       }
     }
}
Peter Kriens
  • 15,196
  • 1
  • 37
  • 55
  • Thank you. Let me try it out. – Program-Me-Rev Apr 04 '16 at 13:52
  • 1
    Thank you so much. That was so much helpful, even though it might seem quite obvious. Much appreciated. Works great. Thanks so much for your work with Bnd Tools. It makes OSGi so much more easier and enjoyable. Great! – Program-Me-Rev Apr 04 '16 at 22:55