java.util.zip.ZipException:打开 zip 文件时出错

发布于 2024-07-09 09:07:45 字数 354 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

暮光沉寂 2024-07-16 09:07:45

确保您的 jar 文件没有损坏。 如果它已损坏或无法解压缩,则会出现此错误。

Make sure your jar file is not corrupted. If it's corrupted or not able to unzip, this error will occur.

掌心的温暖 2024-07-16 09:07:45

我遇到了同样的问题。 我有一个 zip 存档,java.util.zip.ZipFile 无法处理,但 WinRar 可以很好地解压它。 我发现SDN上的文章< /a> 关于 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();
}

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.

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();
}
高跟鞋的旋律 2024-07-16 09:07:45

我以前见过这个异常,当时 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.

梦醒时光 2024-07-16 09:07:45

可能和log4j有关。

websphere java 类路径(如启动文件中定义)以及应用程序类路径中是否有 log4j.jar 文件?

如果您确实确保 log4j.jar 文件位于 java 类路径中,并且不在 Web 应用程序的 web-inf/lib 目录中。


它还可以与 ant版本(可能不是你的情况,但我把它放在这里供参考):

您的类路径中有一个 .class 文件(即不是目录或 .jar 文件)。 从 ant 1.6 开始,ant 将打开类路径中的文件检查清单条目。 此尝试打开将失败,并出现错误“java.util.zip.ZipException”

ant 1.5 不存在该问题,因为它不会尝试打开文件。 - 因此请确保您的类路径不包含 .class 文件。


顺便说一句,您是否考虑过 单独的 jar ?< br>
您可以在主 jar 的清单中,引用具有此属性的其他 jar:

Class-Path: one.jar two.jar three.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):

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.


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:

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.

我们只是彼此的过ke 2024-07-16 09:07:45

我通过清除 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.

你怎么这么可爱啊 2024-07-16 09:07:45

我在 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.

肩上的翅膀 2024-07-16 09:07:45

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.

极度宠爱 2024-07-16 09:07:45

遇到异常

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)
...

我在用 Java 解压存档时 。 存档本身似乎没有损坏,因为 7zip(和其他人)打开它时没有任何问题或关于无效 CRC 的投诉。

我切换到 Apache Commons Compress 来读取 zip 条目,这解决了问题。

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.

I switched to Apache Commons Compress for reading the zip-entries and that resolved the problem.

回忆追雨的时光 2024-07-16 09:07:45

只是为了克服 ZipException,我使用了 commons-compress1.14 名为 jarchivelibthrau 可以轻松地从 File 对象中提取或压缩。

示例:

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 依赖项 « 您可以从 Sonatype Maven 存储库 (位于 org/rauschig/jarchivelib/

<dependency>
  <groupId>org.rauschig</groupId>
  <artifactId>jarchivelib</artifactId>
  <version>0.7.1</version>
</dependency>

@see

Simply to overcome the ZipException's, i have used a wrapper for commons-compress1.14 called jarchivelibwritten by thrau that makes it easy to extract or compress from and into File objects.

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 Sonatype Maven Repository at org/rauschig/jarchivelib/.

<dependency>
  <groupId>org.rauschig</groupId>
  <artifactId>jarchivelib</artifactId>
  <version>0.7.1</version>
</dependency>

@see

你的笑 2024-07-16 09:07:45

在 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.

猛虎独行 2024-07-16 09:07:45

就我而言,我的 -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 my lib directory contains 101 jar files.

蓝色星空 2024-07-16 09:07:45

就我而言,多个版本的 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.

仅此而已 2024-07-16 09:07:45

时遇到此异常。

我在构建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文