我有一个Jar文件,其中包含其他嵌套的Jars.当我JarFile()在这个文件上调用新的构造函数时,我得到一个异常,它说:
java.util.zip.ZipException:打开zip文件时出错
当我手动解压缩此Jar文件的内容并再次压缩它时,它工作正常.
我只在WebSphere 6.1.0.7及更高版本上看到此异常.同样的事情在tomcat和WebLogic上运行良好.
当我使用JarInputStream而不是JarFile时,我能够读取Jar文件的内容,没有任何异常.
小智 16
我遇到了同样的问题.我有一个zip存档,java.util.zip.ZipFile无法处理,但WinRar解压缩得很好.我在SDN上发现了关于压缩和解压缩Java选项的文章.我稍微修改了一个示例代码,以生成最终能够处理存档的方法.Trick正在使用ZipInputStream而不是ZipFile以及顺序读取zip存档.此方法还能够处理空zip存档.我相信您可以调整方法以满足您的需求,因为所有zip类都具有.jar档案的等效子类.
public void unzipFileIntoDirectory(File archive, File destinationDir)
throws Exception {
final int BUFFER_SIZE = 1024;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(archive);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
File destFile;
while ((entry = zis.getNextEntry()) != null) {
destFile = FilesystemUtils.combineFileNames(destinationDir, entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
continue;
} else {
int count;
byte data[] = new byte[BUFFER_SIZE];
destFile.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(destFile);
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
fos.close();
}
}
zis.close();
fis.close();
}
Run Code Online (Sandbox Code Playgroud)
Von*_*onC 10
它可能与log4j有关.
您是否在websphere java类路径(如启动文件中定义)以及应用程序类路径中有log4j.jar文件?
如果确实确保log4j.jar文件位于java类路径中,并且它不在webapp的web-inf/lib目录中.
它也可以与ant版本相关(可能不是你的情况,但我把它放在这里供参考):
您的类路径中有.class文件(即不是目录或.jar文件).从ant 1.6开始,ant将打开类路径中的文件,检查清单条目.此尝试打开将失败,并显示错误"java.util.zip.ZipException"
ant 1.5不存在这个问题,因为它不会尝试打开文件. - 所以请确保您的类路径不包含.class文件.
另外,您是否考虑过单独的罐子?
您可以在主jar的清单中,使用此属性引用其他jar:
Class-Path: one.jar two.jar three.jar
Run Code Online (Sandbox Code Playgroud)
然后,将所有罐子放在同一个文件夹中.
同样,可能对您的案件无效,但仍有参考.