簡體   English   中英

java.util.zip.ZipException:無效的壓縮方法

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

我在處理 Mac OS X 10.7.3 上的 zip 文件時遇到了一些問題。

我從第三方收到一個 zip 文件,我必須對其進行處理。 我的代碼使用 ZipInputStream 來做到這一點。 此代碼之前已多次使用,沒有任何問題,但對於此特定 zip 文件卻失敗了。 我得到的錯誤如下:

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)

我用谷歌搜索了一下,我可以看到 ZipInputStream 存在一些問題,例如這個

我還在 Stackoverflow 上發現了一些相關的問題,例如這個 但是沒有適當的、可接受的/可接受的答案。

我有幾個問題:

  1. 有沒有人為此找到任何具體的解決方案? 像任何后期更新或所有不同的 JAR 一樣,具有相同的功能但沒有問題
  2. 在這個鏈接上,用戶phobuz1提到“如果使用非標准壓縮方法(方法6)” ,就會出現這個問題。 有沒有辦法找出使用哪種壓縮方法? 這樣我才能確定失敗的原因?

請注意,與某些用戶一樣,如果我在本地機器上解壓縮文件並重新壓縮它,它就可以正常工作。

編輯 1:

我得到的文件是.zip格式,我不知道他們使用哪個操作系統/實用程序來壓縮它。 在我的本地機器上,我使用 Mac OS X 附帶的內置 zip 實用程序。

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

根據該接口,您可以獲取和設置壓縮方法(分別為getCompression()setCompression(int) )。

祝你好運!

我在 Windows XP 操作系統上使用 Java 中的以下代碼來壓縮文件夾。 作為旁注,它至少對您有用。

//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;
}

在這段代碼中,您需要通過傳遞兩個參數來調用前面的方法zipFiles(String srcFolder, String destZipFile) 第一個參數表示您要壓縮的文件夾,第二個參數destZipFile表示您的目標 zip 文件夾。


下面的代碼是解壓一個壓縮文件夾。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM