diff --git a/WHATSNEW b/WHATSNEW index e093b0538..382136c52 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -48,9 +48,9 @@ Fixed bugs: explicitly disable caching to avoid problems with reloading jars. Bugzilla Report 54473 - * AntClassloader will now ignore files that are part of the classpath but - not valid zip files when scanning for resources. It used to throw - an exception. + * AntClassloader will now ignore files that are part of the classpath + but not zip files when scanning for resources. It used to throw an + exception. Bugzilla Report 53964 Other changes: diff --git a/src/main/org/apache/tools/ant/AntClassLoader.java b/src/main/org/apache/tools/ant/AntClassLoader.java index 58351f9f2..a6e923146 100644 --- a/src/main/org/apache/tools/ant/AntClassLoader.java +++ b/src/main/org/apache/tools/ant/AntClassLoader.java @@ -41,7 +41,6 @@ import java.util.jar.Attributes.Name; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.Manifest; -import java.util.zip.ZipException; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.util.CollectionUtils; import org.apache.tools.ant.util.FileUtils; @@ -50,6 +49,7 @@ import org.apache.tools.ant.util.LoaderUtils; import org.apache.tools.ant.util.ReflectUtil; import org.apache.tools.ant.util.VectorSet; import org.apache.tools.ant.launch.Locator; +import org.apache.tools.zip.ZipLong; /** * Used to load classes within ant with a different classpath from @@ -1004,19 +1004,14 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener { } else { if (jarFile == null) { if (file.exists()) { - try { - jarFile = new JarFile(file); - } catch (ZipException notAJar) { - // raised if a file that is not a ZIP - // happens to be part of the classpath - - // this obviously cannot contain the - // resource + if (!isZip(file)) { String msg = "CLASSPATH element " + file + " is not a JAR."; log(msg, Project.MSG_WARN); System.err.println(msg); return null; } + jarFile = new JarFile(file); jarFiles.put(file, jarFile); } else { return null; @@ -1574,4 +1569,38 @@ public class AntClassLoader extends ClassLoader implements SubBuildListener { return new AntClassLoader(parent, project, path, parentFirst); } + private static final ZipLong EOCD_SIG = new ZipLong(0X06054B50L); + private static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER = + new ZipLong(0X30304B50L); + + private static boolean isZip(File file) throws IOException { + byte[] sig = new byte[4]; + if (readFully(file, sig)) { + ZipLong start = new ZipLong(sig); + return ZipLong.LFH_SIG.equals(start) // normal file + || EOCD_SIG.equals(start) // empty zip + || ZipLong.DD_SIG.equals(start) // split zip + || SINGLE_SEGMENT_SPLIT_MARKER.equals(start); + } + return false; + } + + private static boolean readFully(File f, byte[] b) throws IOException { + FileInputStream fis = new FileInputStream(f); + try { + final int len = b.length; + int count = 0, x = 0; + while (count != len) { + x = fis.read(b, count, len - count); + if (x == -1) { + break; + } + count += x; + } + return count == len; + } finally { + fis.close(); + } + } + }