Monday, 14 February 2011

Read multiple files from Resource JAR or Disk

following to my further Post about loading one file from a JAR file or Disk, here is a method which loads directly multiple files. it will decide base of given URL, if it must load those files from Disk or a Resource JAR file.
In my sample, I wanted to be more detailed and load just XML files.



    /**
     * Loads multiple xml files from Disk or Resource JAR file.
     *
     @param clazz current class
     @param path package path of resources
     @return the resource listing
     @throws URISyntaxException the URI syntax exception
     @throws IOException Signals that an I/O exception has occurred.
     */
    List<InputStream> loadMultipleResources(final Class<?> clazz, final String paththrows URISyntaxException, IOException {
        final List<InputStream> listOfFiles = new ArrayList<InputStream>();

        // it is a normal directory, so just find and list files with XML extension
        URL directoryUrl = clazz.getClassLoader().getResource(path);
        if (directoryUrl != null && directoryUrl.getProtocol().equals("file")) {
            final File[] files = new File(directoryUrl.toURI()).listFiles(
                    new FilenameFilter() {
                        @Override
                        public boolean accept(final File dir, final String name) {
                            return name.endsWith(".xml");
                        }
                    });

            for (final File f : files)
                listOfFiles.add(new FileInputStream(f));

            return listOfFiles;
        }

        if (directoryUrl == null) {
            final String packageName = clazz.getPackage().getName();
            directoryUrl = clazz.getResource(packageName);
        }

        // the given path is a JAR file, so extract all XML Files from classpath and return their content as stream
        if (directoryUrl.getProtocol().equals("jar")) {
            final String jarPath = directoryUrl.getPath().substring(5, directoryUrl.getPath().indexOf("!"));
            final JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
            final Enumeration<JarEntry> entries = jar.entries();
            while (entries.hasMoreElements()) {
                final String name = entries.nextElement().getName();
                if (name.startsWith(path&& name.endsWith(".xml")) { // filter according to the path
                    String entry = name.substring(path.length());
                    final int checkSubdir = entry.indexOf("/");
                    if (checkSubdir >= 0) {
                        entry = entry.substring(0, checkSubdir);
                    }
                    final InputStream inputStream = clazz.getClassLoader().getResourceAsStream(name);
                    
                    listOfFiles.add(inputStream);
                }
            }

            return listOfFiles;
        }

        throw new UnsupportedOperationException("Cannot list files for URL " + directoryUrl);
    }


No comments:

Post a Comment