java.util.zip.ZipException: invalid stored block lengths

Vinod Jayachandran picture Vinod Jayachandran · Feb 25, 2013 · Viewed 10.1k times · Source

I am trying to read a ZipArchiveEntry to String. I have the below code and i run into exception

My code is as below

StringWriter writer = new StringWriter();
IOUtils.copy(zipFile.getInputStream(zipArchiveEntry),
              writer,
              org.apache.commons.io.Charsets.UTF_8);

And I get the following exception

java.util.zip.ZipException: invalid stored block lengths
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:147)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.Reader.read(Reader.java:123)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2001)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1980)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1957)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1907)

Really stuck at it for a long time. Please help out.

Answer

MosheElisha picture MosheElisha · Jan 21, 2020

I had a similar case of an apparently valid ZIP but had difficulties reading it using Java. The exception was similar to yours but with a different stacktrace:

Caused by: java.util.zip.ZipException: invalid stored block lengths
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:194)
at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:140)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:118)
...

In my case, the "malfunctioning" ZIP was created using an Ansible "archive" module on a CentOS machine:

- name: Create a zip archive
  archive:
    path: /tmp/mydir/
    dest: /tmp/mydir.zip
    format: zip

The fix was to add an asterisk at the end of the "path" when creating the ZIP:

- name: Create a zip archive
  archive:
    path: /tmp/mydir/*
    dest: /tmp/mydir.zip
    format: zip

The content of the two ZIPs is the same but there is some difference in file/directory attributes within the ZIP that apparently cause the issue in Java.