gpt4 book ai didi

java - 如何使用类似于 jar -xvf 的 jar 命令仅列出 jar 内的文件夹将给出所有文件和目录?

转载 作者:行者123 更新时间:2023-12-02 03:52:47 24 4
gpt4 key购买 nike

我有包含不同文件夹、子文件夹和文件的 jar。要列出和查看 jar 的内容,我可以使用 jar -tvf jar_name 。是否有任何方法 jar -tvf 或类似的其他命令将仅列出 jar 内容内的目录。

我看到 jar 有以下选项,但除了 -t 之外,我找不到哪个可以帮助专门列出文件或文件夹。不能使用 -t 一次性全部列出选项。

jar Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file

我需要加载 jar 文件并以树结构显示文件夹和文件。实现这种逻辑的最佳方法是什么?

最佳答案

命令行(Linux);

jar tf JAR_PATH | grep ".*/$"

获取 jar 文件中的目录的 Java 代码;

try {
JarFile jarFile = new JarFile(JAR_PATH);
Enumeration<JarEntry> paths = jarFile.entries();
while (paths.hasMoreElements()) {
JarEntry path = paths.nextElement();
if (path.isDirectory()) {
System.out.println(path.getName());
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于java - 如何使用类似于 jar -xvf 的 jar 命令仅列出 jar 内的文件夹将给出所有文件和目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com