使用 ZipInputStream 读取和修改内部 zip 文件的内容

Read and Modify content of inner zip file using ZipInputStream

我正在尝试使用 ZipInputStream 修改 web.xml。要读取的文件是 web.xml,它位于 ear/war 文件

C:/XX.ear/YY.war/WEB-INF/web.xml

ZipFile zipFile = new ZipFile("C:/XX.ear");
    Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while(entries.hasMoreElements()){
        ZipEntry entry = entries.nextElement();
        if(entry.getName().equalsIgnoreCase("YY.war")){
            ZipInputStream inputStream = new ZipInputStream(zipFile.getInputStream(entry));
            ZipEntry zipEntry;
            while(( zipEntry  = inputStream.getNextEntry())!=null)
            {
                System.out.println(zipEntry.getName());// Prints WEB-INF/web.xml
                if (zipEntry.getName().endsWith(".xml")) {
                    ZipInputStream iStream = new ZipInputStream(zipFile.getInputStream(zipEntry)); 
                      // throws NullPointerException here

                }                   
            }                   
        }                       
    }

new ZipInputStream(zipFile.getInputStream(zipEntry)) 我无法指向内部 zip 文件以获取内部 war 文件的输入流

是否可以将内部 zipentry 读取为输入流??

Java 7 nio2 package has a new file system interface and a new Zip File System provider. The interface and provider allow to traverse the zip tree in a visitor pattern style 这应该提供一种更清晰有效的方式来获取内部 zip 文件

编辑:我试图遍历一个 zip-inside-zip 并得到一个异常。根据this thread,你必须提取内部zip才能遍历它。