16

许多 Java jar 中包含的 manifest.mf 包含看起来很像电子邮件标题的标题。参见示例 [*]

我想要一些可以将这种格式解析为键值对的东西:

Map<String, String> manifest = <mystery-parse-function>(new File("manifest.mf"));

我已经用谷歌搜索了一些关于“parse manifest.mf”“manifest.mf format”等的信息,我发现了大量关于标题含义的信息(例如在 OSGI 包、标准 Java jar 等中),但这不是什么我在找。

查看一些示例 manifest.mf 文件,我可能会实现一些东西来解析它(对格式进行逆向工程),但我不知道我的实现是否真的正确。所以我也不是在寻找其他人的快速组合解析函数,因为它遇到了同样的问题)。

对我的问题的一个好的回答可以指出我的格式规范(这样我就可以编写自己的正确解析函数)。最好的答案将我指向一个已经有正确实现的现有开源库。

[*] = https://gist.github.com/kdvolder/6625725

4

1 回答 1

23

可以使用Manifest类读取 MANIFEST.MF 文件:

Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));

然后你可以通过做

Map<String, Attributes> entries = manifest.getEntries();

以及所有主要属性

Attributes attr = manifest.getMainAttributes();

一个工作示例

我的MANIFEST.MF文件是这样的:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

我的代码:

Manifest manifest = new Manifest(new FileInputStream(new File("MANIFEST.MF")));
Attributes attr = manifest.getMainAttributes();

System.out.println(attr.getValue("Manifest-Version"));
System.out.println(attr.getValue("X-COMMENT"));

输出:

1.0
Main-Class will be added automatically by build
于 2013-09-19T16:08:17.460 回答