[英]How to read a war/jar file which does not have manifest
I have a war file which does not contains manifest not even META-INF
folder. 我有一个不包含清单的战争文件,甚至没有
META-INF
文件夹。 Now my problem is that I wrote a code which was working fine with normal war files containing manifests. 现在我的问题是我写了一个代码,可以正常处理包含清单的常规war文件。 Now I am required to read a war file which does not contain manifest.
现在,我需要阅读一个不包含清单的战争文件。
When I check 当我检查
while ((ze = zis.getNextEntry()) != null)
This condition is just skipped. 仅跳过此条件。 Is there any API which treats it just as a normal zip file or is there any workaround.
是否有将它当作普通zip文件对待的API,或者是否有任何解决方法。
I have tried with JarEntry
as well as ZipEntry
. 我已经尝试过
JarEntry
和ZipEntry
。 Here is a small snippet that should be explanatory. 这是一个说明性的小片段。
try {
FileInputStream fis = new FileInputStream(applicationPack);
ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);
ArchiveEntry ze = null;
File applicationPackConfiguration;
while ((ze = zis.getNextEntry()) != null) {
// do someting
}
What can be done ? 该怎么办?
You can simply list contents with ZipFile class: 您可以使用ZipFile类简单列出内容:
try {
// Open the ZIP file
ZipFile zf = new ZipFile("filename.zip");
// Enumerate each entry
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
// Get the entry name
String zipEntryName = ((ZipEntry)entries.nextElement()).getName();
}
} catch (IOException e) {
}
Example taken from here . 例子取自这里 。 Another example for retrieving the file from zip .
从zip检索文件的另一个示例。
Update: 更新:
Code above indeed has problems with zip files that contain only directory as a top-level element. 上面的代码确实对仅包含目录作为顶级元素的zip文件存在问题。
This code works (tested): 此代码有效(经过测试):
try {
// Open the ZIP file
FileInputStream fis = new FileInputStream(new File("/your.war"));
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry = null;
while ((entry = zis.getNextEntry()) != null)
// Get the entry name
System.out.println(entry.getName());
} catch (IOException e) {
}
You can use classes from java.util.zip package. 您可以使用java.util.zip包中的类。 Just replace ZipArchiveInputStream with ZipInputStream and ArchiveEntry with ZipEntry:
只需将ZipArchiveInputStream替换为ZipInputStream并将ArchiveEntry替换为ZipEntry:
FileInputStream fis = new FileInputStream(new File("/path/to/your.war"));
ZipInputStream zis = new ZipInputStream(fis);
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
System.out.println(ze.getName());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.