java.util.zip.ZipException: invalid compression method

Bhushan picture Bhushan · Mar 13, 2012 · Viewed 9.4k times · Source

I am having some issues in dealing with zip files on Mac OS X 10.7.3.

I am receiving a zip file from a third party, which I have to process. My code is using ZipInputStream to do this. This code has been used several times before, without any issue but it fails for this particular zip file. The error which I get is as follows:

java.util.zip.ZipException: invalid compression method
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:185)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)
    at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)

I googled about it and I can see that there are some issues with ZipInputStream, e.g. this one.

I have also found some related questions on Stackoverflow, e.g. this one. But there is no proper, accepted/acceptable answer.

I have a couple questions:

  1. Has anyone found any concrete solution for this? Like any lates update or an all together different JAR which has same functionality but no issues
  2. On this link, the user phobuz1 mentions that "if non-standard compression method (method 6)" is used, then this problem occurs. Is there a way to find out which compression method is used? So that I can be sure of the reason of failure?

Note that as with some users, if I unzip the file on my local machine and re-zip it, it works perfectly fine.

EDIT 1:

The file which I am getting is in .zip format, I don't know which OS/utility program they are using to compress it. On my local machine I am using the built-in zip utility which comes with Mac OS X.

Answer

Brandt Solovij picture Brandt Solovij · Mar 13, 2012

API JAVADOC : THIS is what actually compresses your file ( Updated Feb 2021 note, the oracle provided link expired, Duke University (no affiliation) however has the antiquated 1.4 javadoc available) : https://www2.cs.duke.edu/csed/java/jdk1.4.2/docs/api/java/util/zip/ZipEntry.html

per that interface, you are able to get and set compression methods ( getCompression() and setCompression(int), respectively).

good luck!