【问题标题】:How to list only folders inside jar using jar cmmand similar to jar -tvf will give all files and directory?如何使用类似于 jar -tvf 的 jar 命令仅列出 jar 中的文件夹将提供所有文件和目录?
【发布时间】:2016-03-02 07:09:22
【问题描述】:

我有包含不同文件夹、子文件夹和文件的 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 文件并以树形结构显示文件夹和文件。实现这种逻辑的最佳方法是什么。

【问题讨论】:

  • 你想用java显示还是只用命令行显示?
  • 我想在 shell 脚本和 bat 文件中使用该功能。
  • 我已经编辑了我的答案,希望对您有所帮助
  • 感谢它为我工作。

标签: java jar


【解决方案1】:

命令行(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();
}

【讨论】:

    【解决方案2】:

    Jar 是一个 zip 文件。使用 this answer 。或以编程方式answer1 或此answer2

    获取文件列表(see docs) 并grep那些以“\”结尾的行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      • 2012-01-17
      • 2010-09-25
      相关资源
      最近更新 更多