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形式ですが、圧縮に使用している OS/ユーティリティ プログラムがわかりません。私のローカル マシンでは、Mac OS X に付属している組み込みの zip ユーティリティを使用しています。

4

2 に答える 2

1

API JAVADOC : これは実際にファイルを圧縮するものです ( 2021 年 2 月に更新されたメモ、オラクルが提供するリンクは期限切れ、デューク大学 (所属なし) には時代遅れの 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 OS の 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)このコードでは、 2 つのパラメーターを渡して前述のメソッドを呼び出す必要があります。最初のパラメーターは圧縮するフォルダーを示し、2 番目のパラメーターdestZipFileは宛先の zip フォルダーを示します。


次のコードは、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 に答える