понедельник, 14 марта 2016 г.

Перечисление ресурсов в определенной папке jar

List<String> result = new ArrayList<String>();
URL dirURL;
String path = "ru/brbpm/lecm/config/eatd/ReportTemplates/audit_report/";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// classLoader = ResourceUtils.class.getClassLoader();
dirURL = classLoader.getResource(path);
  try {
  // некрасивый способ, но в Java6 только так
  if (dirURL != null && (dirURL.toURI().getScheme().equals("jar") || dirURL.toURI().getScheme().equals("wsjar"))) {
      String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); //strip out only the JAR file
      JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
      Enumeration<JarEntry> entries = jar.entries();
      while (entries.hasMoreElements()) {
           String name = entries.nextElement().getName();
           if (name.startsWith(path) && !name.equals(path))
                 result.add(name.substring(path.length(), name.length()));
           }
      }
  } catch (Exception e) {
    logger.error(e.toString(), e);
  }