【问题标题】:Android: opening large zip fileAndroid:打开大型 zip 文件
【发布时间】:2014-12-08 05:49:51
【问题描述】:

我无法解压缩大文件 (50 mb)。 ZipInputStreamZipFile 我都试过了。

当我使用ZipFile 时出现以下异常:

java.util.zip.ZipException: EOCD 未找到;不是 Zip 存档?

当我使用ZipInputStream 时,我收到了跟随错误:

没有 zip 条目(zin.getNextEntry())

ZipInputStream代码:

public static void unzip(File _zipFile, File directory) throws IOException {
    try {
        FileInputStream fin = new FileInputStream(_zipFile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {
            // not getting here
        }
        zin.close();
    }
    catch (Exception e) {
    }
}

ZipFile代码:

public static void unzipa(File zipfile, File directory) throws IOException {
    try {
        ZipFile zfile = new ZipFile(zipfile); // getting exception here
        Enumeration<? extends ZipEntry> entries = zfile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();

        }
        zfile.close();
    }
    catch (Exception ex) {
        ErroHandling.HandleError(ex);
    }

}

【问题讨论】:

标签: android zip zipfile


【解决方案1】:

如果在初始化 ZipFile 时抛出了 ZipExceptionIOException,那么您应该测试 ZIP 的完整性。您可能还想确保您具有读/写访问权限。如果您在 Android 的内部存储 (sdcard) 上解压缩此文件,您需要在 AndroidManifest 中声明以下权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

代码对我来说看起来不错,但这是我快速编写并在大于 100 MB 的有效 ZIP 文件上测试的解决方案:

public static boolean unzip(final File zipFile, final File destinationDir) {
    ZipFile zip = null;
    try {
        zip = new ZipFile(zipFile);
        final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
        while (zipFileEntries.hasMoreElements()) {
            final ZipEntry entry = zipFileEntries.nextElement();
            final String entryName = entry.getName();
            final File destFile = new File(destinationDir, entryName);
            final File destinationParent = destFile.getParentFile();
            if (destinationParent != null && !destinationParent.exists()) {
                destinationParent.mkdirs();
            }
            if (!entry.isDirectory()) {
                final BufferedInputStream is = new BufferedInputStream(
                        zip.getInputStream(entry));
                int currentByte;
                final byte data[] = new byte[2048];
                final FileOutputStream fos = new FileOutputStream(destFile);
                final BufferedOutputStream dest = new BufferedOutputStream(fos, 2048);
                while ((currentByte = is.read(data, 0, 2048)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        }
    } catch (final Exception e) {
        return false;
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (final IOException ignored) {
            }
        }
    }
    return true;
}

【讨论】:

  • 我在 zip = new ZipFile(zipFile); java.util.zip.ZipException:未找到 EOCD;不是 Zip 存档?我可以在桌面上打开 zip 文件。如果我将文件大小减小到 10 mb,我可以打开(从 zip 中删除了一些文件)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 2014-11-07
  • 1970-01-01
  • 2017-04-22
  • 2021-05-02
  • 1970-01-01
相关资源
最近更新 更多