使用索引从 jar 获取文件



我正在尝试使用索引从正在运行的jar访问java.io.File的文件。我的意思是,就像在该文件夹中创建文件数组,然后通过索引获取文件一样。

jar tf file.jar命令可用于列出jar文件的内容。

jar tf 命令使用性的示例:

> jar tf demo-1.0.0.jar
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
...
org/springframework/boot/loader/jar/
org/springframework/boot/loader/jar/JarURL

也可以通过使用java.jar.Jarfile类以编程方式获得相同的结果。

下面是以编程方式查找 jar 文件内容的示例:

// File name: JarLs.java
// This program lists down all the contents of a .jar file
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.Enumeration;
public class JarLs {
public static void main(String[] args) throws Exception {
JarFile jarFile = new JarFile("D:/test/demo-1.0.0.jar");
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
System.out.println(jarEntries.nextElement().getName());
}
}
}

输出:

> javac JarLs.java
> java JarLs
META-INF/
META-INF/MANIFEST.MF
org/
org/springframework/
org/springframework/boot/
org/springframework/boot/loader/
org/springframework/boot/loader/archive/
org/springframework/boot/loader/archive/ExplodedArchive$FileEntry.class
org/springframework/boot/loader/WarLauncher.class
...
...
BOOT-INF/
BOOT-INF/classes/
BOOT-INF/classes/com/
...
...
BOOT-INF/classes/com/demo/controller/WebTrafficController.class
BOOT-INF/classes/com/demo/SpringBootWebApplication.class

更多信息:

https://docs.oracle.com/javase/tutorial/deployment/jar/view.html

http://www.devx.com/tips/java/reading-contents-of-a-jar-file-using-java.-170629013043.html

最新更新