Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.zip.ZipException: error in opening zip file

Tags:

java

zip

unzip

I have a Jar file, which contains other nested Jars. When I invoke the new JarFile() constructor on this file, I get an exception which says:

java.util.zip.ZipException: error in opening zip file

When I manually unzip the contents of this Jar file and zip it up again, it works fine.

I only see this exception on WebSphere 6.1.0.7 and higher versions. The same thing works fine on tomcat and WebLogic.

When I use JarInputStream instead of JarFile, I am able to read the contents of the Jar file without any exceptions.

like image 272
Sandhya Agarwal Avatar asked Nov 28 '08 07:11

Sandhya Agarwal


2 Answers

Make sure your jar file is not corrupted. If it's corrupted or not able to unzip, this error will occur.

like image 193
arulraj.net Avatar answered Oct 19 '22 19:10

arulraj.net


I faced the same problem. I had a zip archive which java.util.zip.ZipFile was not able to handle but WinRar unpacked it just fine. I found article on SDN about compressing and decompressing options in Java. I slightly modified one of example codes to produce method which was finally capable of handling the archive. Trick is in using ZipInputStream instead of ZipFile and in sequential reading of zip archive. This method is also capable of handling empty zip archive. I believe you can adjust the method to suit your needs as all zip classes have equivalent subclasses for .jar archives.

public void unzipFileIntoDirectory(File archive, File destinationDir)      throws Exception {     final int BUFFER_SIZE = 1024;     BufferedOutputStream dest = null;     FileInputStream fis = new FileInputStream(archive);     ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));     ZipEntry entry;     File destFile;     while ((entry = zis.getNextEntry()) != null) {         destFile = FilesystemUtils.combineFileNames(destinationDir, entry.getName());         if (entry.isDirectory()) {             destFile.mkdirs();             continue;         } else {             int count;             byte data[] = new byte[BUFFER_SIZE];             destFile.getParentFile().mkdirs();             FileOutputStream fos = new FileOutputStream(destFile);             dest = new BufferedOutputStream(fos, BUFFER_SIZE);             while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {                 dest.write(data, 0, count);             }             dest.flush();             dest.close();             fos.close();         }     }     zis.close();     fis.close(); } 
like image 32
JohnyCash Avatar answered Oct 19 '22 21:10

JohnyCash