2

我在处理 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 实用程序。

4

2 回答 2

1

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)

祝你好运!

于 2012-03-12T23:38:52.140 回答
1

我在我的 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();
        }
    }
}
于 2012-03-12T23:59:03.100 回答