解析jar中manifest.mf文件条目的正确方法是什么? [英] What is the proper way to parse the entries of a manifest.mf file in jar?

查看:97
本文介绍了解析jar中manifest.mf文件条目的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多Java jar中包含的manifest.mf包含类似于电子邮件标题的标题.参见示例[*]

The manifest.mf contained in many Java jars contains headers which look much like email headers. See example [*]

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

I want something that can parse this format into key value pairs:

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

我在Google上搜索了"parse manifest.mf","manifest.mf格式"等内容,并且发现了很多有关标头含义的信息(例如,在OSGI包,标准Java jar等中),但是那不是我想要的.

I have googled around a bit for "parse manifest.mf" "manifest.mf format" etc. and I find plenty of information about the meaning of the headers (e.g. in OSGI bundles, standard Java jars, etc.) but that's not what I'm looking for.

看一些示例manifest.mf文件,我可能可以实现一些东西来解析它(对格式进行逆向工程),但是我不知道我的实现是否正确.因此,我也不希望其他人会遇到同样的问题而迅速将它们分解为parse函数).

Looking at some example manifest.mf files I could probably implement something to parse it (reverse engineer the format) but I won't know if my implementation is actually correct. So I'm also not looking for someone else's quickly thrown together parse function as it suffers the same problem).

一个很好的答案可以指向格式规范(这样我就可以编写自己的 correct 解析函数).最好的答案是指向现有的开源库,该库已经具有正确的实现.

A good answer to my question could point me to a specification of the format (so I can write my own correct parse function). The best answer points me to an existing open-source library that already has a correct implementation.

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

推荐答案

MANIFEST.MF文件可以使用

MANIFEST.MF files can be read with the Manifest class:

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

然后您可以通过获取所有条目

Then you can get all entries by doing

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

以及所有主要属性

Attributes attr = manifest.getMainAttributes();

一个可行的例子

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

A working example

My MANIFEST.MF file is this:

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

这篇关于解析jar中manifest.mf文件条目的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆