@@ -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();
}
}
}