Minor ZipFile gotcha

java.util.zip.ZipFile and its subclass java.util.jar.JarFile has a minor gotcha when using getEntry(String); leading slashes are not ignored. For example:

final JarFile jarFile = new JarFile("rt.jar");
System.out.println(jarFile.getEntry("/java/lang/Object.class"));
System.out.println(jarFile.getEntry("java/lang/Object.class"));

will return:

null
java/lang/Object.class

In general, this is not a big deal. But when manually parsing URLs (don’t ask) such as:

jar:file://rt.jar!/java/lang/Object.class

it can bite you in the butt.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s