• 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();
                    }
                }
            }
        }
        
    }
    
  • 相关阅读:
    Delphi Canvas的FillRect(const Rect: TRect) 函数的作用
    将多个jpg文件以追加形式合并成一个文件_delphi教程 bmp 合并 http://www.west.cn/www/info/58058-1.htm
    早期原版翎风(LF)引擎(M2)源码(Delphi)
    Application.Restore不起作用了
    delphi 函数isiconic 函数 判断窗口是否最小化
    delphi SetWindowPos改变窗体位置和状态
    Linux 软件看门狗 watchdog
    使用Linux C编写看门狗(watchdog)程序
    Linux编程之《看门狗进程》
    Mac PWN入门巩固篇(六)
  • 原文地址:https://www.cnblogs.com/binarylei/p/9170272.html
Copyright © 2020-2023  润新知