All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.feizns.JarUtils Maven / Gradle / Ivy

There is a newer version: 5.5-RELEASE
Show newest version
package com.gitee.feizns;

import lombok.SneakyThrows;

import java.net.JarURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author feizns
 * @since 2019/5/20
 */
public abstract class JarUtils {

    /**
     * 协议
     */
    public static final String PROTOCOL = "jar";


    /****************************************
     * 判断是否是jar资源
     ****************************************
     * @param url 链接
     * @return url.startsWith("jar")
     */
    public static final boolean isJar(String url) {
        return url.startsWith(PROTOCOL);
    }

    public static final boolean isJar(URL url) {
        return PROTOCOL.equals(url.getProtocol());
    }


    /****************************************
     * 获取jar文件
     ****************************************
     * @param url url link
     * @return java.util.jar.JarFile
     */
    @SneakyThrows
    public static final JarFile jarFile(String url) {
        return jarFile(new URL(url));
    }

    @SneakyThrows
    public static final JarFile jarFile(URL url) {
        return isJar(url) ? ((JarURLConnection) url.openConnection()).getJarFile() : null;
    }

    @SneakyThrows
    public static final JarFile jarFile(Class clz) {
        return jarFile(clz.getResource("").toURI().toURL());
    }


    /****************************************
     * 消费方式.
     ****************************************
     * @param url 链接
     * @param consumer 消费方式
     */
    public static final void consume(String url, Consumer consumer) {
        consume(jarFile(url), consumer);
    }

    @SneakyThrows
    public static final void consume(URL url, Consumer consumer) {
        try ( JarFile jarFile = jarFile(url) ) {
            consume(jarFile, consumer);
        }
    }

    public static final void consume(JarFile jarFile, Consumer consumer) {
        consume(jarFile, item -> true, consumer);
    }


    /****************************************
     * 筛选消费.
     ****************************************
     * @param jarFile java.util.jar.JarFile
     * @param prefix 文件前缀
     * @param consumer 消费方式
     */
    public static final void consume(JarFile jarFile, String prefix, Consumer consumer) {
        consume(jarFile, item -> item.getName().startsWith(prefix), consumer);
    }

    @SneakyThrows
    public static final void consume(String url, String prefix, Consumer consumer) {
        try ( JarFile jarFile = jarFile(url); ) {
            consume(jarFile, item -> item.getName().startsWith(prefix), consumer);
        }
    }

    public static final void consume(JarFile jarFile, Predicate filter, Consumer consumer) {
        Enumeration entries = jarFile.entries();
        while ( entries.hasMoreElements() ) {
            JarEntry item = entries.nextElement();
            if ( filter.test(item) )
                consumer.accept(item);
        }
    }

    @SneakyThrows
    public static final void consume(Class clz, Predicate filter, Consumer consumer) {
        try ( JarFile jarFile = jarFile(clz) ) {
            System.out.println(jarFile);
            consume(jarFile, filter, consumer);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy