如何读取没有清单的war/jar文件

发布于 2024-11-06 07:57:19 字数 701 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-11-13 07:57:19

您可以简单地使用 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) {
}

示例取自 < a href="http://www.exampledepot.com/egs/java.util.zip/ListZip.html" rel="nofollow">此处。 从 zip 检索文件的另一个示例。

更新:

上面的代码确实对仅包含目录作为顶级元素的 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) {
    }

You can simply list contents with ZipFile class:

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.

Update:

Code above indeed has problems with zip files that contain only directory as a top-level element.

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) {
    }
放飞的风筝 2024-11-13 07:57:19

您可以使用 java.lang.Class 中的类。 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());
}

You can use classes from java.util.zip package. Just replace ZipArchiveInputStream with ZipInputStream and ArchiveEntry with 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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文