简体   繁体   English

java.util.zip.ZipException:无效的压缩方法

[英]java.util.zip.ZipException: invalid compression method

I am having some issues in dealing with zip files on Mac OS X 10.7.3.我在处理 Mac OS X 10.7.3 上的 zip 文件时遇到了一些问题。

I am receiving a zip file from a third party, which I have to process.我从第三方收到一个 zip 文件,我必须对其进行处理。 My code is using ZipInputStream to do this.我的代码使用 ZipInputStream 来做到这一点。 This code has been used several times before, without any issue but it fails for this particular zip file.此代码之前已多次使用,没有任何问题,但对于此特定 zip 文件却失败了。 The error which I get is as follows:我得到的错误如下:

java.util.zip.ZipException: invalid compression method
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:185)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:105)
    at org.apache.xerces.impl.XMLEntityManager$RewindableInputStream.read(Unknown Source)
    at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
    at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)

I googled about it and I can see that there are some issues with ZipInputStream, eg this one .我用谷歌搜索了一下,我可以看到 ZipInputStream 存在一些问题,例如这个

I have also found some related questions on Stackoverflow, eg this one .我还在 Stackoverflow 上发现了一些相关的问题,例如这个 But there is no proper, accepted/acceptable answer.但是没有适当的、可接受的/可接受的答案。

I have a couple questions:我有几个问题:

  1. Has anyone found any concrete solution for this?有没有人为此找到任何具体的解决方案? Like any lates update or an all together different JAR which has same functionality but no issues像任何后期更新或所有不同的 JAR 一样,具有相同的功能但没有问题
  2. On this link , the user phobuz1 mentions that "if non-standard compression method (method 6)" is used, then this problem occurs.在这个链接上,用户phobuz1提到“如果使用非标准压缩方法(方法6)” ,就会出现这个问题。 Is there a way to find out which compression method is used?有没有办法找出使用哪种压缩方法? So that I can be sure of the reason of failure?这样我才能确定失败的原因?

Note that as with some users, if I unzip the file on my local machine and re-zip it, it works perfectly fine.请注意,与某些用户一样,如果我在本地机器上解压缩文件并重新压缩它,它就可以正常工作。

EDIT 1:编辑 1:

The file which I am getting is in .zip format, I don't know which OS/utility program they are using to compress it.我得到的文件是.zip格式,我不知道他们使用哪个操作系统/实用程序来压缩它。 On my local machine I am using the built-in zip utility which comes with Mac OS X.在我的本地机器上,我使用 Mac OS X 附带的内置 zip 实用程序。

API JAVADOC : THIS is what actually compresses your file ( Updated Feb 2021 note, the oracle provided link expired, Duke University (no affiliation) however has the antiquated 1.4 javadoc available) : https://www2.cs.duke.edu/csed/java/jdk1.4.2/docs/api/java/util/zip/ZipEntry.html API JAVADOC:这是实际压缩您的文件的内容( 2021 年 2 月更新的注释,oracle 提供的链接已过期,杜克大学(无隶属关系)但提供过时的 1.4 javadoc): https : //www2.cs.duke.edu/csed /java/jdk1.4.2/docs/api/java/util/zip/ZipEntry.html

per that interface, you are able to get and set compression methods ( getCompression() and setCompression(int) , respectively).根据该接口,您可以获取和设置压缩方法(分别为getCompression()setCompression(int) )。

good luck!祝你好运!

I'm using the following code in Java on my Windows XP OS to zip folders.我在 Windows XP 操作系统上使用 Java 中的以下代码来压缩文件夹。 It may at least be useful to you as a side note.作为旁注,它至少对您有用。

//add folder to the zip file
private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception
{
    File folder = new File(srcFolder);

    //check the empty folder
    if (folder.list().length == 0)
    {
        System.out.println(folder.getName());
        addFileToZip(path , srcFolder, zip,true);
    }
    else
    {
        //list the files in the folder
        for (String fileName : folder.list())
        {
            if (path.equals(""))
            {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip,false);
            }
            else
            {
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip,false);
            }
        }
    }
}

//recursively add files to the zip files
private void addFileToZip(String path, String srcFile, ZipOutputStream zip,boolean flag)throws Exception
{
    //create the file object for inputs
    File folder = new File(srcFile);
    //if the folder is empty add empty folder to the Zip file
    if (flag==true)
    {
        zip.putNextEntry(new ZipEntry(path + "/" +folder.getName() + "/"));
    }
    else
    {
         //if the current name is directory, recursively traverse it to get the files
        if (folder.isDirectory())
        {
            addFolderToZip(path, srcFile, zip); //if folder is not empty
        }
        else
        {
            //write the file to the output
            byte[] buf = new byte[1024];
            int len;
            FileInputStream in = new FileInputStream(srcFile);
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));

            while ((len = in.read(buf)) > 0)
            {
                zip.write(buf, 0, len); //Write the Result
            }
        }
    }
}

//zip the folders
private void zipFolder(String srcFolder, String destZipFile) throws Exception
{
    //create the output stream to zip file result
    FileOutputStream fileWriter = new FileOutputStream(destZipFile);
    ZipOutputStream zip = new ZipOutputStream(fileWriter);
    //add the folder to the zip
    addFolderToZip("", srcFolder, zip);
    //close the zip objects
    zip.flush();
    zip.close();
}

private boolean zipFiles(String srcFolder, String destZipFile) throws Exception
{
    boolean result=false;
    System.out.println("Program Start zipping the given files");
    //send to the zip procedure
    zipFolder(srcFolder,destZipFile);
    result=true;
    System.out.println("Given files are successfully zipped");
    return result;
}

In this code, you need to invoke the preceding method zipFiles(String srcFolder, String destZipFile) by passing two parameters.在这段代码中,您需要通过传递两个参数来调用前面的方法zipFiles(String srcFolder, String destZipFile) The first parameter indicates your folder to be zipped and the second parameter destZipFile indicates your destination zip folder.第一个参数表示您要压缩的文件夹,第二个参数destZipFile表示您的目标 zip 文件夹。


The following code is to unzip a zipped folder.下面的代码是解压一个压缩文件夹。

private void unzipFolder(String file) throws FileNotFoundException, IOException
{
    File zipFile=new File("YourZipFolder.zip");
    File extractDir=new File("YourDestinationFolder");

    extractDir.mkdirs();

    ZipInputStream inputStream = new ZipInputStream(new FileInputStream(zipFile));

    try
    {
        ZipEntry entry;
        while ((entry = inputStream.getNextEntry()) != null)
        {
            StringBuilder sb = new StringBuilder();
            sb.append("Extracting ");
            sb.append(entry.isDirectory() ? "directory " : "file ");
            sb.append(entry.getName());
            sb.append(" ...");
            System.out.println(sb.toString());

            File unzippedFile = new File(extractDir, entry.getName());
            if (!entry.isDirectory())
            {
                if (unzippedFile.getParentFile() != null)
                {
                    unzippedFile.getParentFile().mkdirs();
                }

                FileOutputStream outputStream = new FileOutputStream(unzippedFile);

                try
                {
                    byte[] buffer = new byte[1024];
                    int len;

                    while ((len = inputStream.read(buffer)) != -1)
                    {
                        outputStream.write(buffer, 0, len);
                    }
                }
                finally
                {
                    if (outputStream != null)
                    {
                        outputStream.close();
                    }
                }
            }
            else
            {
                unzippedFile.mkdirs();
            }
        }
    }
    finally
    {
        if (inputStream != null)
        {
            inputStream.close();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在放心的代码中收到“java.util.zip.ZipException:未知压缩方法” - I am getting "java.util.zip.ZipException: unknown compression method" in my restassured code java.util.zip.ZipException:无效的通用标志:9 - java.util.zip.ZipException: invalid general purpose flag: 9 原因:java.util.zip.ZipException:设置的代码长度无效 - Caused by: java.util.zip.ZipException: invalid code lengths set java.util.zip.ZipException:CEN标头无效(签名错误) - java.util.zip.ZipException: invalid CEN header (bad signature) java.util.zip.ZipException:使用 SentenceModel 设置的代码长度无效 - java.util.zip.ZipException: invalid code lengths set with SentenceModel java.util.zip.ZipException:无效的存储块长度 - java.util.zip.ZipException: invalid stored block lengths java.util.zip.ZIPException: 不是 GZIP 格式 - java.util.zip.ZIPException: Not in GZIP format 修复 java.util.zip.ZipException - fixing java.util.zip.ZipException 关于Retrofit和GSON的java.util.zip.ZipException - java.util.zip.ZipException on Retrofit and GSON 解压缩Java中导致“ java.util.zip.ZipException”的zip文件-无效的LOC标头(错误的签名) - Unpacking zip files in Java that cause a “java.util.zip.ZipException” - invalid LOC header (bad signature)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM