开发者

How to read a war/jar file which does not have manifest

开发者 https://www.devze.com 2023-03-03 23:22 出处:网络
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

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 a开发者_开发知识库s 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 ?


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) {
    }


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());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消