Advertisement
broken-arrow

Untitled

Dec 27th, 2021
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1.     public static List<String> getFilenamesForDirnameFromCP(String directoryName) throws
  2.             URISyntaxException, IOException {
  3.         List<String> filenames = new ArrayList<>();
  4.  
  5.         URL url = CustomContainersMainClass.class.getClassLoader().getResource(directoryName);
  6.  
  7.         if (url != null) {
  8.             if (url.getProtocol().equals("file")) {
  9.                 File file = Paths.get(url.toURI()).toFile();
  10.                 if (file != null) {
  11.                     File[] files = file.listFiles();
  12.                     if (files != null) {
  13.                         for (File filename : files) {
  14.                             filenames.add(filename.toString());
  15.                         }
  16.                     }
  17.                 }
  18.             } else if (url.getProtocol().equals("jar")) {
  19.                 String dirname = directoryName + "/";
  20.                 String path = url.getPath();
  21.                 String jarPath = path.substring(5, path.indexOf("!"));
  22.                 try (JarFile jar = new JarFile(URLDecoder.decode(jarPath, StandardCharsets.UTF_8.name()))) {
  23.                     Enumeration<JarEntry> entries = jar.entries();
  24.                     while (entries.hasMoreElements()) {
  25.                         JarEntry entry = entries.nextElement();
  26.                         String name = entry.getName();
  27.                         //System.out.println("name " + name + "entry " + entry + jarPath);
  28.                         if (name.startsWith(dirname) && !dirname.equals(name)) {
  29.                             URL resource = CustomContainersMainClass.class.getClassLoader().getResource(name);
  30.                             if (resource != null) {
  31.                                 filenames.add(name);
  32.                             } else
  33.                                 Common.log("Missing files in plugins/" + CustomContainersMainClass.getInstance() + ".jar/" + directoryName + "/, contact the author of " + CustomContainersMainClass.getInstance().getName() + ".");
  34.                         }
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.         return filenames;
  40.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement