java.util.zip.ZipException:打开 zip 文件时出错
我有一个 Jar 文件,其中包含其他嵌套的 Jar。 当我在此文件上调用新的 JarFile()
构造函数时,出现一个异常:
java.util.zip.ZipException:打开 zip 文件时出错
当我手动解压缩此 Jar 文件的内容并再次将其压缩时,它工作正常。
我只在 WebSphere 6.1.0.7 及更高版本上看到此异常。 同样的事情在 tomcat 和 WebLogic 上也可以正常工作。
当我使用 JarInputStream 而不是 JarFile 时,我能够毫无例外地读取 Jar 文件的内容。
I have a Jar file, which contains other nested Jars. When I invoke the new JarFile()
constructor on this file, I get an exception which says:
java.util.zip.ZipException: error in opening zip file
When I manually unzip the contents of this Jar file and zip it up again, it works fine.
I only see this exception on WebSphere 6.1.0.7 and higher versions. The same thing works fine on tomcat and WebLogic.
When I use JarInputStream instead of JarFile, I am able to read the contents of the Jar file without any exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
确保您的 jar 文件没有损坏。 如果它已损坏或无法解压缩,则会出现此错误。
Make sure your jar file is not corrupted. If it's corrupted or not able to unzip, this error will occur.
我遇到了同样的问题。 我有一个 zip 存档,java.util.zip.ZipFile 无法处理,但 WinRar 可以很好地解压它。 我发现SDN上的文章< /a> 关于 Java 中的压缩和解压缩选项。 我稍微修改了示例代码之一以生成最终能够处理存档的方法。 技巧在于使用 ZipInputStream 而不是 ZipFile 并顺序读取 zip 存档。 此方法还能够处理空的 zip 存档。 我相信您可以调整该方法以满足您的需求,因为所有 zip 类都有 .jar 档案的等效子类。
I faced the same problem. I had a zip archive which java.util.zip.ZipFile was not able to handle but WinRar unpacked it just fine. I found article on SDN about compressing and decompressing options in Java. I slightly modified one of example codes to produce method which was finally capable of handling the archive. Trick is in using ZipInputStream instead of ZipFile and in sequential reading of zip archive. This method is also capable of handling empty zip archive. I believe you can adjust the method to suit your needs as all zip classes have equivalent subclasses for .jar archives.
我以前见过这个异常,当时 JVM 认为的 temp 目录由于不存在或没有写入权限而无法访问。
I've seen this exception before when whatever the JVM considers to be a temp directory is not accessible due to not being there or not having permission to write.
可能和log4j有关。
websphere java 类路径(如启动文件中定义)以及应用程序类路径中是否有 log4j.jar 文件?
如果您确实确保 log4j.jar 文件位于 java 类路径中,并且不在 Web 应用程序的 web-inf/lib 目录中。
它还可以与 ant版本(可能不是你的情况,但我把它放在这里供参考):
顺便说一句,您是否考虑过 单独的 jar ?< br>
您可以在主 jar 的清单中,引用具有此属性的其他 jar:
然后,将所有 jar 放在同一个文件夹中。
同样,可能不适用于您的情况,但仍然可供参考。
It could be related to log4j.
Do you have log4j.jar file in the websphere java classpath (as defined in the startup file) as well as the application classpath ?
If you do make sure that the log4j.jar file is in the java classpath and that it is NOT in the web-inf/lib directory of your webapp.
It can also be related with the ant version (may be not your case, but I do put it here for reference):
On a side note, did you consider having separate jars ?
You could in the manifest of your main jar, refer to the other jars with this attribute:
Then, place all of your jars in the same folder.
Again, may be not valid for your case, but still there for reference.
我通过清除 jboss-xyz/server[config]/tmp 和 jboss-xyz/server/[config]/work 目录解决了这个问题。
I solved this by clearing the jboss-x.y.z/server[config]/tmp and jboss-x.y.z/server/[config]/work directories.
我在 Java 6 的特定 Zip 文件中看到了这一点,但当我升级到 Java 8 时(没有测试 Java 7),它就消失了,因此 Java 中的 ZipFile 的新版本似乎支持更多压缩算法,因此可以读取以下文件早期版本失败。
I saw this with a specific Zip-file with Java 6, but it went away when I upgrade to Java 8 (did not test Java 7), so it seems newer versions of ZipFile in Java support more compression algorithms and thus can read files which fail with earlier versions.
Liquibase 为我收到此错误。 我在调试并观察 liquibase 尝试加载库后解决了这个问题,发现它在 commons-codec-1.6.jar 的清单文件上出错。 本质上,您的路径中某处存在损坏的 zip 文件,或者正在使用不兼容的版本。 当我对该库的 Maven 存储库进行探索时,我发现有较新的版本并将较新的版本添加到 pom.xml 中。 此时我能够继续进行。
Liquibase was getting this error for me. I resolved this after I debugged and watched liquibase try to load the libraries and found that it was erroring on the manifest files for commons-codec-1.6.jar. Essentially, there is either a corrupt zip file somewhere in your path or there is a incompatible version being used. When I did an explore on Maven repository for this library, I found there were newer versions and added the newer version to the pom.xml. I was able to proceed at this point.
遇到异常
我在用 Java 解压存档时 。 存档本身似乎没有损坏,因为 7zip(和其他人)打开它时没有任何问题或关于无效 CRC 的投诉。
我切换到 Apache Commons Compress 来读取 zip 条目,这解决了问题。
I was getting exception
when unzipping an archive in Java. The archive itself didn't seem corrupted as 7zip (and others) opened it without any problems or complaints about invalid CRC.
I switched to Apache Commons Compress for reading the zip-entries and that resolved the problem.
只是为了克服 ZipException,我使用了
commons-compress
1.14 名为jarchivelib
由 thrau 可以轻松地从 File 对象中提取或压缩。示例:
Maven 依赖项 « 您可以从 Sonatype Maven 存储库 (位于 org/rauschig/jarchivelib/。
@see
Simply to overcome the ZipException's, i have used a wrapper for
commons-compress
1.14 calledjarchivelib
written by thrau that makes it easy to extract or compress from and into File objects.Example:
Maven dependency « You can download the jars from the Sonatype Maven Repository at org/rauschig/jarchivelib/.
@see
在 Windows7 上,我通过 Samba 网络连接处理大于 80 MB 的 Java8 Jar 文件时遇到了这个问题。 将文件复制到本地驱动器解决了该问题。
On Windows7 I had this problem over a Samba network connection for a Java8 Jar File >80 MBytes big. Copying the file to a local drive fixed the issue.
就我而言,我的
-Dloader.path="lib"
包含其他不需要的 jar。例如,
mvn dependency:copy-dependencies
列出了 100 个 jar 文件。但是我的lib
目录包含 101 个 jar 文件。In my case , my
-Dloader.path="lib"
contains other jars that doesn't need.for example,
mvn dependency:copy-dependencies
lists 100 jar files.but mylib
directory contains 101 jar files.就我而言,多个版本的 SL4j-api.jar 在 Maven 存储库中存在冲突。 我删除了 m2 maven repo 中的整个 SL4j-api 文件夹并更新了 maven 项目,构建了 maven 项目,然后在 JBOSS 服务器中运行了该项目。 问题得到解决。
In my case SL4j-api.jar with multiple versions are conflicting in the maven repo. Than I deleted the entire SL4j-api folder in m2 maven repo and updated maven project, build maven project than ran the project in JBOSS server. issue got resolved.
时遇到此异常。
我在构建 mvn clean install -DskipTests ZipException opening "groovy-4.0.18.jar": zip END header not found
删除 groovy-4.0.18.jar 并重建对我有用
I was getting this exception while building mvn clean install -DskipTests
ZipException opening "groovy-4.0.18.jar": zip END header not found
deleting groovy-4.0.18.jar and rebuilding worked for me.