• 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();
                    }
                }
            }
        }
        
    }
    
  • 相关阅读:
    git push 失败出现error: src refspec master does not match any.解决方案
    git:not a git repository (or any of the parent directories)
    初次安装git配置用户名和邮箱
    MYSQL的C API之mysql_query
    PyCharm + PyQt4 环境搭建
    pycharm下打开、执行并调试scrapy爬虫程序
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/binarylei/p/9170272.html
Copyright © 2020-2023  润新知