user1915244
user1915244

Reputation: 21

Extracting zip file into a folder throws "Invalid entry size (expected 46284 but got 46285 bytes)" for one of the entry

When I am trying to extract the zip file into a folder as per the below code, for one of the entry (A text File) getting an error as "Invalid entry size (expected 46284 but got 46285 bytes)" and my extraction is stopping abruptly. My zip file contains around 12 text files and 20 TIF files. It is encountering the problem for the text file and is not able to proceed further as it is coming into the Catch block. I face this problem only in Production Server which is running on Unix and there is no problem with the other servers(Dev, Test, UAT). We are getting the zip into the servers path through an external team who does the file transfer and then my code starts working to extract the zip file.

    ...
    int BUFFER = 2048;
    java.io.BufferedOutputStream dest = null;
    String ZipExtractDir = "/y34/ToBeProcessed/";
    java.io.File MyDirectory = new java.io.File(ZipExtractDir);
    MyDirectory.mkdir();
    ZipFilePath = "/y34/work_ZipResults/Test.zip";

    // Creating fileinputstream for zip file
    java.io.FileInputStream fis = new java.io.FileInputStream(ZipFilePath);

    // Creating zipinputstream for using fileinputstream
    java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(new java.io.BufferedInputStream(fis));
    java.util.zip.ZipEntry entry;
    while ((entry = zis.getNextEntry()) != null)
    {
        int count;
        byte data[] = new byte[BUFFER];
        java.io.File f = new java.io.File(ZipExtractDir + "/" + entry.getName());

        // write the files to the directory created above
        java.io.FileOutputStream fos = new java.io.FileOutputStream(ZipExtractDir + "/" + entry.getName());
        dest = new java.io.BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) != -1)
        {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
    }
    zis.close();
    zis.closeEntry();
}
catch (Exception Ex)
{
    System.Out.Println("Exception in \"ExtractZIPFiles\"---- " + Ex.getMessage());
}

Upvotes: 2

Views: 6458

Answers (2)

sp00m
sp00m

Reputation: 48817

I can't understand the problem you're meeting, but here is the method I use to unzip an archive:

public static void unzip(File zip, File extractTo) throws IOException {
    ZipFile archive = new ZipFile(zip);
    Enumeration<? extends ZipEntry> e = archive.entries();
    while (e.hasMoreElements()) {
        ZipEntry entry = e.nextElement();
        File file = new File(extractTo, entry.getName());
        if (entry.isDirectory()) {
            file.mkdirs();
        } else {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            InputStream in = archive.getInputStream(entry);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            IOUtils.copy(in, out);
            in.close();
            out.close();
        }
    }
}

Calling:

File zip = new File("/path/to/my/file.zip");
File extractTo = new File("/path/to/my/destination/folder");
unzip(zip, extractTo);

I never met any issue with the code above, so I hope that could help you.

Upvotes: 3

atripathi
atripathi

Reputation: 908

Off the top of my head, I could think of these reasons:

  1. There could be problem with the encoding of the text file.
  2. The file needs to be read/transferred in "binary" mode.
  3. There could be an issue with the line ending \n or \r\n
  4. The file could simply be corrupt. Try opening the file with a zip utility.

Upvotes: 2

Related Questions