问题描述
我有一个Servlet,用户可以在其上传.jar文件以检查其 MANIFEST.MF
。
I've got a Servlet on which a user can upload a .jar file to check its MANIFEST.MF
.
我知道以下内容可行:
JarInputStream in = new JarInputStream(new ByteArrayInputStream (fileItem.get()));
Manifest mf = in.getManifest();
不幸的是 getManifest()
方法解析关键:值和需要:(冒号+空格),简单的冒号是不够的。由于我正在使用的手机也可以工作,如果 MANIFEST.MF
只有冒号而不是冒号+空格,我想手动提取MANIFEST.MF,但我不想将文件保存在磁盘上。
Unfortunately the getManifest()
method parses for Key: Value and needs the ": " (colon+space), a simple colon is not enough. Since the mobile phones I'm working with also work if the MANIFEST.MF
only has colons and not colon+space, I'd like to extract the MANIFEST.MF manually, but I don't want to save the file on disk.
如果我有一个JarFile,我可以通过以下方式解析清单:
If I'd have a JarFile, I could parse the Manifest via:
JarFile jarFile = new JarFile(fileName);
InputStream in = jarFile.getInputStream(jarFile.getEntry("META-INF/MANIFEST.MF"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
String[] splitAr = line.split(":", 0);
// ...
但不幸的是我不知道如何转换 JarInputStream
(或 ByteArrayInputStream
)到 JarFile
。
But unfortunately I have no idea how I could convert a JarInputStream
(or a ByteArrayInputStream
) to a JarFile
.
推荐答案
在这种情况下我会尝试做什么:
What I would try to do in this case:
- 使用
getNextEntry()
遍历jar中的所有条目 - 不是getNextJarEntry()
!并且看看你是否可以通过这种方式到达清单 - 尝试使用
ZipInputStream
而不是更通用 -JarInputStream
实际上扩展了它 - 这应该让你有权获得META-INF / MANIFEST.MF
文件。
- iterate through all the entries in the jar using
getNextEntry()
-- notgetNextJarEntry()
! and see if you can reach the manifest this way - try to use a
ZipInputStream
instead which is more generic --JarInputStream
in fact extends it -- and that should give you access to get theMETA-INF/MANIFEST.MF
file.
这篇关于如何在不保存到磁盘的情况下将JarInputStream转换为JarFile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!