如何判断一个文件是jar文件
1.一般是读取他的名字,然后查看后缀名字就可以了,
2.这样是一种方法,但是还有一种可能,就是该文件没有.jar
的后缀的文件
那么判断方法如下:
private static final byte[] JAR_MAGIC = { 'P', 'K', 3, 4 };
protected boolean isJar(URL url) {
return isJar(url, new byte[JAR_MAGIC.length]);
}
protected boolean isJar(URL url, byte[] buffer) {
InputStream is = null;
try {
is = url.openStream();
is.read(buffer, 0, JAR_MAGIC.length);
if (Arrays.equals(buffer, JAR_MAGIC)) {//判断开始字节是不是jar包
if (log.isDebugEnabled()) {
log.debug("Found JAR: " + url);
}
return true;
}
} catch (Exception e) {
// Failure to read the stream means this is not a JAR
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// Ignore
}
}
}
return false;
}
这样就可以判断该文件是不是jar包了,那么同理,其他压缩文件也可以用这个判断