Java JarFile 解析
2024-08-27 05:43:56
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();
}
}
}
}
}
最新文章
- 关于ckeditor ajax提交到后台 问题
- jQuery 学习之路(1):引子
- font-family属性与字体对齐
- PHP对大小写敏感问题的处理比较乱,写代码时可能偶尔出问题,所以这里总结一下。以便用到的出现错误
- nginx限制ip请求次数 以及并发次数
- HDU2602 (0-1背包问题)
- 数据库操作封装类 DBHelper.cs
- iOS 开发的几种手势
- Unity3DGUI:常用控件
- 安全性测试之防范 DDoS 攻击
- [20180904]工作中一个错误.txt
- Fluent动网格【6】:部件变形案例
- 笔记 Bioinformatics Algorithms Chapter7
- tomcat 防火墙如何设置
- windows下用qemu搭建android
- android 读书笔记 1
- [UE4]组件
- Java变量初始化之后的默认值问题
- Python【经典类与新式类】
- xpath简单应用
热门文章
- MemSQL start[c]up Round 1 B题
- Spring集成Mybatis(Dao方式开发)
- linux shell获取用户输入
- Chrome 解决flash问题
- highlight.js 设置行号
- 在 Linux redis 验证交互连接过程中遇到 redis Could not connect to Redis at 127.0.0.1:6379: Connection refused 的解决方法
- PHPstorm配置PHPunit对composer引入的php代码进行单元测试
- SpringIoc 和 工厂模式(反射实现)
- 腾讯EC .net API对接第三方系统
- Maven的依赖机制介绍