ZipFile is throwing error but ZipInputStream is able to decompress the archive
Asked Answered
T

2

7

I am experiencing a strange behavior with java.util.zip.*

I have a zip file and upon decompressing follwing tihngs happen

ZipFile zipfile = new ZipFile(file, ZipFile.OPEN_READ);

This is exaxt error message

java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:127)
at java.util.zip.ZipFile.<init>(ZipFile.java:143)
at com.basware.ExtractZip.unpack(ExtractZip.java:27)
at com.basware.ExtractZip.main(ExtractZip.java:17)

But if I use the following code it is able to open the archive without any errors

try {
     BufferedOutputStream dest = null;       
     File file = new File("File_Path");        
     FileInputStream fis = new FileInputStream(file);
     ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
     ZipEntry entry;
     while((entry = zis.getNextEntry()) != null) {
        System.out.println("Extracting: " +entry);
        int count;
        byte data[] = new byte[BUFFER];
        // write the files to the disk
        FileOutputStream fos = new 
      FileOutputStream(entry.getName());
        dest = new 
          BufferedOutputStream(fos, BUFFER);
        while ((count = zis.read(data, 0, BUFFER)) 
          != -1) {
           dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
     }
     zis.close();

Please note that files are compressed using WinZIP.

My question is as ZipFile and ZipInputStream are almost same ,why ZipFile is giving exception and why it is unable to perform decompression.

EDIT : The problem is if I zip the file using WinZip tool and then decompress it using listed program it is working fine.But, this problem is specifically coming for archives coming from external source(external source claims that they are using WinZip).On top of it, if I open the very same archive(external one) using WinZip tool it is showing and decompressing files.But this JAVA specific code(ZipFile) is not working at all.

EDIT: I am not able to figure it out why java native code is not working for my ZIP archives, but apache compress solved my problem.It is working for me as suggested by Ian Roberts.

Toft answered 10/12, 2012 at 8:59 Comment(0)
O
4

ZipFile attempts to parse the "central directory" at the end of the zip in order to build up a data structure that allows you to access individual entries by name. ZipInputStream doesn't, it only looks at the local header of each entry as it reads through the file from top to bottom. So it looks like your file has good entries but a corrupted central directory for some reason.

There are a number of possibilities, for example issues with the encoding of non-ASCII characters in entry names, or if the zip has more than 64k entries. I would try the commons-compress implementation of ZipFile - even if it doesn't work it should give you a more specific error message than the "something is wrong" that you get from java.util.zip.

Operose answered 10/12, 2012 at 9:20 Comment(2)
@Toft that's one of the problems with the part-native zip implementation in Java, its error messages are not very helpful... I've added a few more ideas.Operose
I tried using org.apache.commons.compress.archivers.zip.ZipFile and it is able to show me the entries, not the errors.But some exceptions- There is no getName method,entries has been renamed to getEntries,getEntries and getEntry return org.apache.commons.compress.archivers.zip.ZipArchiveEntry instances,close is allowed to throw IOException.Toft
U
0

In addition to Ian Robert's answer, if Java 7 is an option, you may wish to sidestep the older java.util.zip libraries in favor of using the ZIP filesystem provider.

Uraeus answered 10/12, 2012 at 13:21 Comment(1)
Sorry.We are not using Java 7Toft

© 2022 - 2024 — McMap. All rights reserved.