Java JarFile 解析

Java JarFile 解析

package com.github.binarylei;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * @author: leigang
 * @version: 2018-06-11
 */
public class JarFileUtils {

    // 获取 clazz 所在 jar 包路径
    public static String getJarPath(Class clazz) {

        String filePath = null;
        URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
        try {
            // 转化为utf-8编码,支持中文
            filePath = URLDecoder.decode(url.getPath(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        File file = new File(filePath);
        filePath = file.getAbsolutePath();  //得到windows下的正确路径
        return filePath;
    }

    // 获取 clazz 所在 jar 包目录
    public static String getJarDir(Class clazz) {
        String jarPath = getJarPath(clazz);
        if (jarPath.endsWith(".jar")) {    // 可执行jar包运行的结果里包含".jar"
            // 获取jar包所在目录
            jarPath = jarPath.substring(0, jarPath.lastIndexOf("/") + 1);
        }
        return jarPath;
    }

    // 遍历 jar 包中的所有文件
    public static List<String> listJarFile(String path) throws IOException {
        JarFile jarFile = new JarFile(path);
        Enumeration<JarEntry> entries = jarFile.entries();
        ArrayList<String> files = new ArrayList<>();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            if (!jarEntry.isDirectory()) {
                files.add("jar:file:/" + path + "!/" + jarEntry.getName());
            }
        }
        return files;
    }

    // 遍历指定 jar 包目录中的文件
    public static List<String> listJarFile(String path, String dir) throws IOException {
        if (!dir.endsWith("/")) {
            dir += "/";
        }
        JarFile jarFile = new JarFile(path);
        Enumeration<JarEntry> entries = jarFile.entries();
        ArrayList<String> files = new ArrayList<>();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            String filename = jarEntry.getName();
            if (!jarEntry.isDirectory() && filename.startsWith(dir)) {
                files.add("jar:file:/" + path + "!/" + filename);
            }
        }
        return files;
    }

    // jar:file:/F:/test-1.0.0.jar!/dir/test.properties
    public static void read(String jarFilePath) {

        InputStream in = null;
        try {
            in =  new URL(jarFilePath).openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String s="";
            while((s=br.readLine()) != null)
                System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}
原文地址:https://www.cnblogs.com/binarylei/p/9170272.html