使用 ZipInputStream 解压缩永远不会完成

Unzipping with ZipInputStream never finishes

我正在使用 AsyncTask 解压缩文件,一切似乎都进行得很顺利(ZIP 存档中的所有文件都已提取),但我的解压缩方法从未完成。

这是我解压的来源 class:

public class MyUnzipper {


    public static boolean unzipFileIntoDirectory(String inputFilename, String outputPath) throws Exception {
        ZipInputStream zis = null;
        BufferedOutputStream dest = null;

        try {
            File archive = new File(inputFilename);
            File destinationDir = new File(outputPath);

            final int BUFFER_SIZE = 1024;

            zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(archive), BUFFER_SIZE));
            ZipEntry entry = null;
            File destFile;
            while( (entry = zis.getNextEntry()) != null ){

                destFile = new File(destinationDir, entry.getName());

                if( entry.isDirectory() ){
                    destFile.mkdirs();
                }
                else {
                    // check for and create parent directories if they don't exist
                    File parentDir = destFile.getParentFile();
                    if ( null != parentDir ) {
                        if ( !parentDir.isDirectory() ) {
                            parentDir.mkdirs();
                        }
                    }

                    int count;
                    byte data[] = new byte[BUFFER_SIZE];
                    dest = new BufferedOutputStream(new FileOutputStream(destFile), BUFFER_SIZE);
                    Log.i("MyUnzipper", "Beginning unzip of " + destFile);

                    while( (count = zis.read(data, 0, BUFFER_SIZE)) != -1 ){ 
                        dest.write(data, 0, count);
                        Log.v("MyUnzipper", "Count = " + count);
                    }
                    dest.flush();
                    dest.close();
                    dest = null;
                }

                zis.closeEntry();

                Log.wtf("MyUnzipper", "Unzipped entry " + entry.getName());
            }

            Log.wtf("MyUnzipper", "Unzip done");
        }
        catch(Exception e){
            Log.wtf("MyUnzipper", "Unzip error");
            e.printStackTrace();
            return false;
        }
        finally {
            if( null != zis )
                zis.close();

            if( null != dest )
                dest.close();
        }

        return true;
    }

}

当我逐行调试时,它运行良好,直到它解压缩所有文件,然后它到达 zis.closeEntry(),而调试器只是 "goes away",即下一行 ( Log.wtf(...)) 永远不会执行。我的 AsyncTask 永远不会完成,就好像我陷入了无限循环?!但是查看 ZipInputStream.closeEntry() 的来源似乎没有任何循环或任何可疑的地方?

我也曾尝试使用 ZipFile 而不是 ZipInputStream 提取 ZIP 存档,但随后出现以下错误:

java.util.zip.ZipException: End Of Central Directory signature not found

ZIP 文件没有问题,我已经在 Mac OSx 上用 zip -v -T 测试过了。我还尝试使用 ZIP 版本 3.0 和 2.1(原始版本为 2.0)重新压缩它。我可以在 Mac OSx 上毫无问题地解压所有版本(使用 Unarchiver 和 Archive Utility)。

这让我抓狂,可能有什么问题?

更新(已解决)

原来是个很蠢的问题,跟解压没有关系

我在解压缩之前从服务器下载 ZIP 文件,显然我忘记在开始解压缩操作之前在下载操作的输出流上调用 close()

也许这个话题可以帮助犯同样愚蠢错误的其他人。

好吧,有时即使您关闭所有以前的输出和输入流,您的 ZIP 提取代码也会卡住。而且,这是 a known bugZipInputStream#read 可以 return 0.

加法:

如果您的 ZIP 文件包含一些非 ACSII 文件名的文件,您将面临解压问题。 Android 的 ZipInputStream 不适用于 UTF-8、CP437 等。

在这种情况下,Apache Commons 应该是一个解决方案:

private boolean unpack(File zipFile, File targetDir) {
    ZipFile zip = null;
    try {
        zip = new ZipFile(zipFile.getAbsoluteFile());
        final Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        while(entries.hasMoreElements()) {
            ZipArchiveEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                mkdirsOrThrow(new File(targetDir, entry.getName()));
                continue;
            }
            final File entryDestination = new File(targetDir,  entry.getName());
            mkdirsOrThrow(entryDestination.getParentFile());
            final InputStream in = zip.getInputStream(entry);
            final OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (zip!= null) try {
            zip.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}