简体   繁体   中英

Unzip Error java.io.EOFException with java unzip code

i am getting this error when i try to unzip a large zip file of about 56MB it works fine for smaller zip files. still new to java so take it easy on me please

java.io.EOFException: Unexpected end of ZLIB input stream
at java.util.zip.InflaterInputStream.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at UnZip.unZipIt(UnZip.java:62)
at UnZip.main(UnZip.java:23)

and the code i am using that gives me this:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;





public class UnZip
{
    List<String> fileList;
    private static final String INPUT_ZIP_FILE = "PAQ-Temp/Downloads/mods.zip";
    private static final String OUTPUT_FOLDER = "PAQ-Temp/images";

    public static void main( String[] args )
    {
        UnZip unZip = new UnZip();
        unZip.unZipIt(INPUT_ZIP_FILE,OUTPUT_FOLDER);
    }


      //Unzip it
      //@param zipFile input zip file
      //@param output zip file output folder

    public void unZipIt(String zipFile, String outputFolder){

    byte[] buffer = new byte[104512585];

     try{

        //create output directory is not exists
        File folder = new File(OUTPUT_FOLDER);
        if(!folder.exists()){
            folder.mkdir();
        }

        //get the zip file content
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while(ze!=null){

           String fileName = ze.getName();
           File newFile = new File(outputFolder + File.separator + fileName);

           System.out.println("file unzip : "+ newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);             

            int len;
            while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
            }

            fos.close();   
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();

        System.out.println("Done unziping");

    }catch(IOException ex){
       ex.printStackTrace(); 
    }
   }   
} 

edit: zip is valid tested with 7zip and windows unzipping both

Are all the files that are in the zip file non-empty? My guess is one of the zip entries has a file size of 0.

Adding a check to see if there are bytes to read to the while loop like this (warning not tested) might work:

 while (zis.available() >0 && (len = zis.read(buffer)) > 0) {

I believe your problem is that you are using the read() method from the FilterInputStream superclass. ZipInputStream has a method read(byte[] buffer, int offset, int length) , which is what you should be using:

while ((len = zis.read(buffer,0,buffer.length)) > 0) {
    fos.write(buffer, 0, len);
}

Since ZipinputStream does not override FilterInputStream#read(byte[] buffer) it probably knows nothing about decompressing the data or respecting entry boundaries.

I do not know if this applies, I'll offer it for what it's worth -- I was unzipping large files programmatically at one time and found a bug in the unzipping classes. It was pretty deep, but the stack trace led me there eventually -- I remember it had to do with using a buffer and incorrectly handling something like the filling of the buffer before the entire block of bytes-to-be-unzipped had been input.

But this info is old, I don't remember which library I was using, it may not apply. I solved it by unzipping all the files I needed before use, so I didn't follow it closely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM