Java // 解压错误 :MALFORMED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20013091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:19:20  来源:igfitidea点击:

Java // unzip error :MALFORMED

javaunzipmalformed

提问by user2998243

I would like to unzip recursively some archive .zip. I use java.util.zip and I can't use an other library.

我想递归解压缩一些存档 .zip。我使用 java.util.zip 并且我不能使用其他库。

My code :

我的代码:

public static void unzip(String file) {

    try {
        File fSourceZip = new File(file);
        String zipPath = file.substring(0, file.length() - 4);
        File temp = new File(zipPath);
        temp.mkdir();
        System.out.println(zipPath + " created");


        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());

            destinationFilePath.getParentFile().mkdirs();

            if (entry.isDirectory()) {
                continue;
            } else {
                System.out.println("Extracting " + destinationFilePath);

                BufferedInputStream bis = new BufferedInputStream(
                        zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];


                FileOutputStream fos = new FileOutputStream(
                        destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos,
                        1024);

                while ((b = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, b);
                }

                bos.flush();
                bos.close();
                bis.close();
            }
            if (entry.getName().endsWith(".zip")) {
                // found a zip file, try to open
                unzip(destinationFilePath.getAbsolutePath());
            }
        }

    } catch (IOException ioe) {
        System.out.println("IOError :" + ioe);
    }
}

But I have some error with some archive :

但是我对一些存档有一些错误:

Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
    at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
    at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567)
    at java.util.zip.ZipFile.access0(ZipFile.java:61)
    at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525)
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500)
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481)
    at zip.ReadingArchive.unzip(ReadingArchive.java:36)
    at zip.ReadingArchive.unzip(ReadingArchive.java:82)
    at zip.ReadingArchive.unzip(ReadingArchive.java:82)
    at main.Main.main(Main.java:13)

I have this problem because there is .odp in my archive. How I can say only uses .zip, not other files ? How I can resolve this problem ?

我有这个问题,因为我的档案中有 .odp。我怎么能说只使用 .zip,而不是其他文件?我该如何解决这个问题?

Thanks !

谢谢 !

回答by Slava Semushin

I've just fixed it by specifying alternative (non UTF-8) charset:

我刚刚通过指定替代(非 UTF-8)字符集来修复它:

Charset CP866 = Charset.forName("CP866");
ZipFile zipFile = new ZipFile(zipArchive, CP866);

In your case, you need to specify another charset. Try, CP437, for instance.

在您的情况下,您需要指定另一个字符集。尝试,CP437例如。