You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用commons-io 2.6解析Zip流时遇ZLIB输入流意外结束异常

Fixing EOFException with ZipInputStream and IOUtils.toByteArray

That EOFException: Unexpected end of ZLIB input stream error happens because you're trying to read the entire ZipInputStream at once using IOUtils.toByteArray(), but ZipInputStream is built to process entries one by one. Each zip entry has its own compressed data segment— the stream doesn't contain a single continuous ZLIB stream. When you read past the end of an entry's data, the ZLIB decoder throws this error because it can't find the expected end of the compressed segment.

Here's how to fix your unzipStream method to properly handle each entry in the zip file:

private static void unzipStream(ZipInputStream zis) throws IOException {
    ZipEntry entry;
    // Iterate through each entry in the zip archive
    while ((entry = zis.getNextEntry()) != null) {
        if (!entry.isDirectory()) { // Skip directory entries if you don't need them
            // Read only the data for the current entry (ZipInputStream handles stopping at the entry boundary)
            byte[] entryData = IOUtils.toByteArray(zis);
            
            // Replace this with your actual processing logic for the entry data
            System.out.printf("Processed entry: %s | Data size: %d bytes%n", entry.getName(), entryData.length);
        }
        // Close the current entry to prepare the stream for the next one
        zis.closeEntry();
    }
}

Key Takeaways:

  • Always use getNextEntry() to move between zip entries—this positions the stream at the start of the entry's compressed data.
  • Reading from ZipInputStream after calling getNextEntry() will automatically stop at the end of that entry's data, so IOUtils.toByteArray() works safely here.
  • Don't forget to call closeEntry() after processing each entry to clean up resources and advance the stream correctly.

Additional Troubleshooting:

If you still get the error after updating the code:

  • Verify your zip file isn't corrupted (try opening it with a standard zip utility to confirm it's intact).
  • Check for conflicting library versions in your classpath—ensure Apache Commons IO 2.6 is the only version being used.

内容的提问来源于stack exchange,提问作者R. Karlus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:38:09