zoukankan      html  css  js  c++  java
  • 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();
                    }
                }
            }
        }
        
    }
    
  • 相关阅读:
    获取最外层View
    Activity的lanuchmode
    decorview that was originally added here or java.lang.IllegalArgumentException: View not attached to window manager
    Android开源项目
    Android屏幕适配
    android获取根视图
    Nginx 安装 和 特性介绍
    kubernetes Pod控制器
    kubernetes 资源清单定义入门
    kubernetes 应用快速入门
  • 原文地址:https://www.cnblogs.com/binarylei/p/9170272.html
Copyright © 2011-2022 走看看