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



我有一个战争文件,它不包含清单,甚至不包含META-INF文件夹。现在我的问题是我编写了一个代码,该代码在包含清单的正常战争文件中工作正常。现在我需要读取一个不包含清单的战争文件。

当我检查时

while ((ze = zis.getNextEntry()) != null)

只是跳过了此条件。是否有任何 API 将其视为普通的 zip 文件,或者是否有任何解决方法。

我已经尝试过JarEntryZipEntry.这里有一个小片段,应该是解释性的。

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检索文件的示例。

更新:

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

最新更新