简体   繁体   English

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

[英]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 . 我已经尝试过JarEntryZipEntry 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.

相关问题 如何在其 jar 依赖项中读取战争文件清单? - How to read war-file manifest in its jar-dependency? 如何在.war manifest.mf文件中引用jar文件(此jar文件是其他war文件的一部分) - how to refer a jar(this jar is part of other war file) file in .war manifest.mf file 如何从耳朵/战争/罐子中读取文件? - How to read file from ear/war/jar? Java从jar中读取文件,该文件位于战争的lib文件夹中 - Java read a file from jar which is in lib folder of a war 如何在战争中添加清单文件 - How to add manifest file in war 如何从* .war / META-INF / MANIFEST.MF文件中读取manifest.mf? - How to read manifest.mf from *.war/META-INF/MANIFEST.MF file? 部署不同应用程序的多次war时,tomcat容器如何区分要读取的war文件的web.xml - how does tomcat container differentiate which web.xml of war file to read when multiple war of different application is deployed 使用Java读取.jar清单文件 - Using Java to read a .jar manifest file 如果jar文件引发异常,如何在批处理文件中处理.jar文件(System.exit()没有任何退出代码)的输出? - How to handle the output of .jar file(which does not have any exit code with System.exit() ) in a batch file if the jar file throws an exception? 如何从战争包装的罐子中读取PNG - How to Read PNG from a jar packaged in a war
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM