JDK-6290451 : new Zipfile() should throw FileNotFoundException when appropriate
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.jar
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-06-24
  • Updated: 2011-02-16
  • Resolved: 2009-06-03
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_02-b09)
Java HotSpot(TM) Client VM (build 1.5.0_02-b09, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
SunOS s9test 5.9 Generic_112233-11 sun4u sparc SUNW,Ultra-4
SunOS s8test 5.8 Generic_108528-17 sun4u sparc SUNW,Ultra-4
(Bug appears to be cross-platform)

A DESCRIPTION OF THE PROBLEM :
According to the javadocs, the constructor of Zipfile() is supposed to throw a ZipFileException "if a ZIP format error has occurred" and is supposed to throw some other type of IOException otherwise.

However, if the file does not exist, it is a ZipException that is thrown, not a FileNotFoundException.

This leads to the inconsistency that attempting a URL.openStream() on any of the following URLs throws FileNotFoundException:

http://www.sun.com/no_such_file_for_bug.jar
jar:http://www.sun.com/no_such_file_for_bug.jar!/entry
file:/c:/no_such_file_for_bug.jar

But on this URL, FileNotFoundException is NOT thrown; instead, a ZipException is thrown:

jar:file:/c:/no_such_file_for_bug.jar!/entry

This bug, if fixed, would also fix bug 4897356 (assuming that the filename is included as with every other FileNotFoundException) and would go most of the way towards fixing bug 5032358.

It's probably a matter of taking the native code that implements ZipFile.open() and adding the error handling code from the native code that implements FileInputStream.open().

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the test case source code without arguments

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I would expect to see the same Exception thrown for each case.
ACTUAL -
I get a different Exception type, with no file name attached, for the last URL.
For URL http://www.sun.com/no_such_file_for_bug.jar
  java.io.FileNotFoundException: http://www.sun.com/no_such_file_for_bug.jar
For URL jar:http://www.sun.com/no_such_file_for_bug.jar!/entry
  java.io.FileNotFoundException: http://www.sun.com/no_such_file_for_bug.jar
For URL file:/c:/no_such_file_for_bug.jar
  java.io.FileNotFoundException: c:\no_such_file_for_bug.jar (The system cannot
find the file specified)
For URL jar:file:/c:/no_such_file_for_bug.jar!/entry
  java.util.zip.ZipException: The system cannot find the file specified


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.*;
import java.net.*;

public class DemoBadURLExceptions {
  static String defaultURLs[] = {
    "http://www.sun.com/no_such_file_for_bug.jar",
    "jar:http://www.sun.com/no_such_file_for_bug.jar!/entry",
    "file:/c:/no_such_file_for_bug.jar",
    "jar:file:/c:/no_such_file_for_bug.jar!/entry"
  };
  public static void main(String [] args)
  {
    String urlStrings[] = (args.length == 0) ? defaultURLs : args;
    for (int i = 0; i < urlStrings.length; i++)
    {
      System.out.println("For URL " + urlStrings[i]);
      try {
        URL url = new URL(urlStrings[i]);
        url.openStream();

        System.out.println("  no exception thrown");
      } catch (Exception e) {
        System.out.println("  " + e);
      }
      System.out.println("");
    }
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Every place in your code that calls URL.openStream() and catches FileNotFoundException for some specialized error handling (and possible message to the user) must also catch ZipException and somehow distinguish between a corrupt zip file and a typoed file name based perhaps on the string message.
###@###.### 2005-06-24 13:59:30 GMT