如何读取没有清单的war/jar文件
我有一个 war 文件,它不包含清单,甚至不包含 META-INF
文件夹。现在我的问题是我编写了一个代码,该代码可以很好地处理包含清单的普通战争文件。现在我需要读取一个不包含清单的战争文件。
当我检查
while ((ze = zis.getNextEntry()) != null)
此条件时,会被跳过。是否有任何 API 将其视为普通的 zip 文件,或者是否有任何解决方法。
我尝试过使用 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
}
可以做什么?
I have a war file which does not contains manifest not even META-INF
folder. Now my problem is that I wrote a code which was working fine with normal war files containing manifests. 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.
I have tried with JarEntry
as well as 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地使用 ZipFile 类列出内容:
示例取自 < a href="http://www.exampledepot.com/egs/java.util.zip/ListZip.html" rel="nofollow">此处。 从 zip 检索文件的另一个示例。
更新:
上面的代码确实对仅包含目录作为顶级元素的 zip 文件存在问题。
此代码有效(已测试):
You can simply list contents with ZipFile class:
Example taken from here. Another example for retrieving the file from zip.
Update:
Code above indeed has problems with zip files that contain only directory as a top-level element.
This code works (tested):
您可以使用 java.lang.Class 中的类。 util.zip 包。只需将 ZipArchiveInputStream 替换为 ZipInputStream,将 ArchiveEntry 替换为 ZipEntry:
You can use classes from java.util.zip package. Just replace ZipArchiveInputStream with ZipInputStream and ArchiveEntry with ZipEntry: