集册 Java实例教程 按Jar扫描资源名称

按Jar扫描资源名称

欢马劈雪     最近更新时间:2020-01-02 10:19:05

433
按Jar扫描资源名称

/*

 * Copyright Bruce Liang (ldcsaa@gmail.com)

 *

 * Version   : JessMA 3.5.1

 * Author   : Bruce Liang

 * Website   : http://www.jessma.org

 * Project   : http://www.oschina.net/p/portal-basic

 * Blog      : http://www.cnblogs.com/ldcsaa

 * WeiBo   : http://weibo.com/u/1402935851

 * QQ Group   : 75375912

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *      http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

import java.io.File;
/* from 
N o w J a v a . c o m - 时  代  Java*/

import java.io.FileFilter;

import java.io.IOException;

import java.net.JarURLConnection;

import java.net.URL;

import java.util.Arrays;

import java.util.Enumeration;

import java.util.HashSet;

import java.util.List;

import java.util.Set;

import java.util.jar.JarEntry;

import java.util.jar.JarFile;

import java.util.regex.Pattern;


public class Main{
/**
 * N o w J a v a . c o m 提 供 
**/

    private static final char PACKAGE_SEP_CHAR = '.';

    private static final char PATH_SEP_CHAR = '/';

    private static final String PATH_SEP_STR = "/";

    private static final String EMPTY_STR = "";

    

    public static final void scanResourceNamesByJar(URL url,

            String basePackagePath, final boolean recursive,

            Set<String> names) {

        scanResourceNamesByJar(url, basePackagePath, recursive, null, names);

    }

    

    public static final void scanResourceNamesByJar(URL url,

            String basePackagePath, final boolean recursive,

            final ResourceFilter filter, Set<String> names) {

        basePackagePath = adjustBasePackagePath(basePackagePath);


        try {

            JarFile jar = ((JarURLConnection) url.openConnection())

                    .getJarFile();

            Enumeration<JarEntry> entries = jar.entries();


            while (entries.hasMoreElements()) {

                JarEntry entry = entries.nextElement();

                String name = entry.getName();

                int nameIndex = name.lastIndexOf(PATH_SEP_CHAR);


                if (entry.isDirectory()

                        || !name.startsWith(basePackagePath))

                    continue;


                if (!recursive && nameIndex != basePackagePath.length())

                    continue;


                String packagePath = (nameIndex != -1 ? name.substring(0,

                        nameIndex) : EMPTY_STR);

                String simpleName = (nameIndex != -1 ? name

                        .substring(nameIndex + 1) : name);


                if (filter != null

                        && !filter.accept(packagePath, simpleName))

                    continue;


                names.add(name);

            }

        } catch (IOException e) {

   
展开阅读全文