javazipzipinputstreamzipexception

How to solve ZipException: Invalid Block lengths when reading ZipInputStream with empty directory inside zip file (Java)


I am getting the error "ZipException: Invalid Block lengths" when reading a ZipInputStream from my java code.

My zip file oppens and is extracted normally with my Zip file utility from windows, so it is not corrupted.

The first entry inside the zip file is an empty directory named "." (is a zip file generated from other system that I don't have access to). When the code tries to get the next entry after the the "." directory, the exception is thrown.

I am getting this file from a SQL Database and don't want to store it in my file system before reading. Here is my code:

try{
    ZipEntry ze = null;
    while((ze = zin.getNextEntry()) != null){ //zin is the ZipInputStream
        if(!ze.isDirectory()){
            // DO SOMETHING WITH THE FILE
        }
    }   
} catch (ZipException ex){
    # EXCEPTION THROWN
} finally {
    zin.close();
}

Solution

  • I don't know if it is the best way to solve it, but I managed to ignore the empty directory entry with Reflections:

        try{
        ZipEntry ze = null;
        while((ze = zin.getNextEntry()) != null){
            if(!ze.isDirectory()){
                // DO SOMETHING WITH THE FILE
            } else if(ze.getName().startsWith(".")){ //CHECK IF IS THE AFOREMENTIONED DIRECTORY
                // MAKE THE ENTRY NULL, THUS THE ZIPINPUTSTREAM WILL NEED TO FECTH ANOTHER
                Field entry = zin.getClass().getDeclaredField("entry");
                entry.setAccessible(true);
                entry.set(zin, null);
                
                // MARK THE END OF THE DIRECTORY ENTRY                                                
                Field eof = zin.getClass().getDeclaredField("entryEOF");
                eof.setAccessible(true);
                eof.set(zin, true);
            }
        }   
    } catch (ZipException ex){
        // EXCEPTION IS NOT THROWN ANYMORE
    } finally {
        zin.close();
    }