[英]How to read a war/jar file which does not have manifest
我有一个不包含清单的战争文件,甚至没有META-INF
文件夹。 现在我的问题是我写了一个代码,可以正常处理包含清单的常规war文件。 现在,我需要阅读一个不包含清单的战争文件。
当我检查
while ((ze = zis.getNextEntry()) != null)
仅跳过此条件。 是否有将它当作普通zip文件对待的API,或者是否有任何解决方法。
我已经尝试过JarEntry
和ZipEntry
。 这是一个说明性的小片段。
try {
FileInputStream fis = new FileInputStream(applicationPack);
ZipArchiveInputStream zis = new ZipArchiveInputStream(fis);
ArchiveEntry ze = null;
File applicationPackConfiguration;
while ((ze = zis.getNextEntry()) != null) {
// do someting
}
该怎么办?
您可以使用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) {
}
更新:
上面的代码确实对仅包含目录作为顶级元素的zip文件存在问题。
此代码有效(经过测试):
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) {
}
您可以使用java.util.zip包中的类。 只需将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.