Java Resource Load getResourcesFromJarFile(final File file, final Pattern pattern)

Here you can find the source of getResourcesFromJarFile(final File file, final Pattern pattern)

Description

Gets the resources from jar file.

License

Mozilla Public License

Parameter

Parameter Description
file the file
pattern the pattern

Return

the resources from jar file

Declaration

private static Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) 

Method Source Code

//package com.java2s;
/**//from w w  w.ja  va 2s.c  o m
 * Copyright (c) 2016 TermMed SA
 * Organization
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/
 */

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;

import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

public class Main {
    /**
     * Gets the resources from jar file.
     *
     * @param file the file
     * @param pattern the pattern
     * @return the resources from jar file
     */
    private static Collection<String> getResourcesFromJarFile(final File file, final Pattern pattern) {
        final ArrayList<String> retval = new ArrayList<String>();
        ZipFile zf;
        try {
            zf = new ZipFile(file);
        } catch (final ZipException e) {
            throw new Error(e);
        } catch (final IOException e) {
            throw new Error(e);
        }
        final Enumeration e = zf.entries();
        while (e.hasMoreElements()) {
            final ZipEntry ze = (ZipEntry) e.nextElement();
            final String fileName = ze.getName();
            final boolean accept = pattern.matcher(fileName).matches();
            if (accept) {
                retval.add(fileName);
            }
        }
        try {
            zf.close();
        } catch (final IOException e1) {
            throw new Error(e1);
        }
        return retval;
    }
}

Related

  1. getResources(final Pattern pattern)
  2. getResourceScripts(String path)
  3. getResourcesFromDirectory(File directory)
  4. getResourcesFromDirectory(final File directory, final Pattern pattern)
  5. getResourcesFromJarFile(File file, String resName)
  6. getResourcesFromZip(final byte[] barContent)
  7. getResourceSize(ClassLoader classLoader, String path)
  8. getResourceStream(Class clazz, String resourceName)
  9. getResourceStream(final String resource)