import java.io.*;
import java.util.jar.*;
import java.util.Enumeration;
public class JarManager {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("用法: java JarManager <JAR路径> [操作]");
System.out.println("操作: list(列出内容), info(查看信息)");
return;
}
String jarPath = args[0];
String operation = args.length > 1 ? args[1] : "info";
try {
switch (operation) {
case "list":
listJarContents(jarPath);
break;
case "info":
default:
showJarInfo(jarPath);
break;
}
} catch (Exception e) {
System.err.println("操作失败: " + e.getMessage());
e.printStackTrace();
}
}
public static void showJarInfo(String jarPath) throws IOException {
try (JarFile jarFile = new JarFile(jarPath)) {
System.out.println("JAR文件信息: " + jarPath);
System.out.println("文件大小: " + jarFile.size() + " 字节");
Manifest manifest = jarFile.getManifest();
if (manifest != null) {
System.out.println("\n清单信息:");
manifest.write(System.out);
} else {
System.out.println("\n无清单信息");
}
}
}
public static void listJarContents(String jarPath) throws IOException {
try (JarFile jarFile = new JarFile(jarPath)) {
System.out.println("JAR包内容列表 (" + jarPath + "):");
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
System.out.println((entry.isDirectory() ? "D " : "F ") + entry.getName());
}
}
}
}