java.util.zip.ZipException:打开 zip 文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/325202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.zip.ZipException: error in opening zip file
提问by Sandhya Agarwal
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:
我有一个 Jar 文件,其中包含其他嵌套的 Jars。当我JarFile()
在这个文件上调用新的构造函数时,我得到一个异常,它说:
java.util.zip.ZipException: error in opening zip file
java.util.zip.ZipException:打开 zip 文件时出错
When I manually unzip the contents of this Jar file and zip it up again, it works fine.
当我手动解压缩这个 Jar 文件的内容并再次压缩它时,它工作正常。
I only see this exception on WebSphere 6.1.0.7 and higher versions. The same thing works fine on tomcat and WebLogic.
我只在 WebSphere 6.1.0.7 和更高版本上看到这个异常。同样的事情在 tomcat 和 WebLogic 上运行良好。
When I use JarInputStream instead of JarFile, I am able to read the contents of the Jar file without any exceptions.
当我使用 JarInputStream 而不是 JarFile 时,我能够毫无例外地读取 Jar 文件的内容。
回答by VonC
It could be related to log4j.
它可能与 log4j 有关。
Do you have log4j.jar file in the websphere java classpath (as defined in the startup file) as well as the application classpath ?
您在 websphere java 类路径(如启动文件中定义)以及应用程序类路径中是否有 log4j.jar 文件?
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.
如果您确实确保 log4j.jar 文件在 java 类路径中并且它不在您的 web 应用程序的 web-inf/lib 目录中。
It can also be related with the ant version(may be not your case, but I do put it here for reference):
它也可能与ant 版本有关(可能不是您的情况,但我确实将其放在这里以供参考):
You have a .class file in your class path (i.e. not a directory or a .jar file). Starting with ant 1.6, ant will open the files in the classpath checking for manifest entries. This attempted opening will fail with the error "java.util.zip.ZipException"
The problem does not exist with ant 1.5 as it does not try to open the files. - so make sure that your classpath's do not contain .class files.
您的类路径中有一个 .class 文件(即不是目录或 .jar 文件)。从 ant 1.6 开始,ant 将打开类路径中的文件检查清单条目。此尝试打开将失败并显示错误“java.util.zip.ZipException”
ant 1.5 不存在此问题,因为它不会尝试打开文件。- 所以请确保您的类路径不包含 .class 文件。
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:
附带说明一下,您是否考虑过使用单独的罐子?
您可以在主 jar 的清单中,引用具有此属性的其他 jar:
Class-Path: one.jar two.jar three.jar
Then, place all of your jars in the same folder.
Again, may be not valid for your case, but still there for reference.
然后,将所有罐子放在同一个文件夹中。
同样,可能对您的情况无效,但仍可供参考。
回答by Artur...
I've seen this exception before when whatever the JVM considers to be a tempdirectory is not accessible due to not being there or not having permission to write.
当 JVM 认为是临时目录的任何内容由于不在那里或没有写入权限而无法访问时,我之前已经看到过这个异常。
回答by arulraj.net
Make sure your jar file is not corrupted. If it's corrupted or not able to unzip, this error will occur.
确保您的 jar 文件没有损坏。如果它已损坏或无法解压缩,则会发生此错误。
回答by Marius K
I solved this by clearing the jboss-x.y.z/server[config]/tmp and jboss-x.y.z/server/[config]/work directories.
我通过清除 jboss-xyz/server[config]/tmp 和 jboss-xyz/server/[config]/work 目录解决了这个问题。
回答by JohnyCash
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 SDNabout 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.
我遇到了同样的问题。我有一个 java.util.zip.ZipFile 无法处理的 zip 存档,但 WinRar 解压它就好了。我在 SDN 上找到了关于 Java 中压缩和解压缩选项的文章。我稍微修改了一个示例代码以生成最终能够处理存档的方法。技巧在于使用 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();
}
回答by centic
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.
我在 Java 6 的特定 Zip 文件中看到了这一点,但是当我升级到 Java 8 时它就消失了(没有测试 Java 7),所以似乎 Java 中的 ZipFile 的新版本支持更多的压缩算法,因此可以读取文件早期版本失败。
回答by iowatiger08
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.
Liquibase 为我收到此错误。我在调试并观察 liquibase 尝试加载库并发现它在 commons-codec-1.6.jar 的清单文件上出错后解决了这个问题。本质上,您的路径中某处存在损坏的 zip 文件,或者使用了不兼容的版本。当我对这个库的 Maven 存储库进行探索时,我发现有更新的版本并将更新的版本添加到 pom.xml。我能够在这一点上继续。
回答by radoh
I was getting exception
我得到了例外
java.util.zip.ZipException: invalid entry CRC (expected 0x0 but got 0xdeadface)
at java.util.zip.ZipInputStream.read(ZipInputStream.java:221)
at java.util.zip.ZipInputStream.closeEntry(ZipInputStream.java:140)
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:118)
...
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.
在 Java 中解压缩存档时。存档本身似乎没有损坏,因为 7zip(和其他人)打开它时没有任何问题或关于无效 CRC 的投诉。
I switched to Apache Commons Compressfor reading the zip-entries and that resolved the problem.
我切换到Apache Commons Compress来读取 zip 条目并解决了问题。
回答by Yash
Simply to overcome the ZipException's, i have used a wrapper for commons-compress
1.14called jarchivelib
written by thrauthat makes it easy to extract or compress from and into File objects.
只要克服抛出:ZipException的,我已经使用的包装1.14所谓书面thrau,可以很容易提取或压缩从进入File对象。commons-compress
jarchivelib
Example:
例子:
public static void main(String[] args) {
String zipfilePath =
"E:/Selenium_Server/geckodriver-v0.19.0-linux64.tar.gz";
//"E:/Selenium_Server/geckodriver-v0.19.0-win32.zip";
String outdir = "E:/Selenium_Server/";
exratctFileList(zipfilePath, outdir );
}
public void exratctFileList( String zipfilePath, String outdir ) throws IOException {
File archive = new File( zipfilePath );
File destinationDir = new File( outdir );
Archiver archiver = null;
if( zipfilePath.endsWith(".zip") ) {
archiver = ArchiverFactory.createArchiver( ArchiveFormat.ZIP );
} else if ( zipfilePath.endsWith(".tar.gz") ) {
archiver = ArchiverFactory.createArchiver( ArchiveFormat.TAR, CompressionType.GZIP );
}
archiver.extract(archive, destinationDir);
ArchiveStream stream = archiver.stream( archive );
ArchiveEntry entry;
while( (entry = stream.getNextEntry()) != null ) {
String entryName = entry.getName();
System.out.println("Entery Name : "+ entryName );
}
stream.close();
}
Maven dependency ? You can download the jars from the SonatypeMaven Repository at org/rauschig/jarchivelib/.
Maven 依赖?您可以从位于org/rauschig/jarchivelib/的SonatypeMaven 存储库下载 jar 。
<dependency>
<groupId>org.rauschig</groupId>
<artifactId>jarchivelib</artifactId>
<version>0.7.1</version>
</dependency>
@see
@看
回答by Wolfgang Fahl
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.
在 Windows7 上,对于大于 80 MB 的 Java8 Jar 文件,我在 Samba 网络连接上遇到了这个问题。将文件复制到本地驱动器修复了该问题。