java - 解压缩 zip 文件超过 100% 时的百分比计算!

标签 java zip progress

我正在解压缩一个 zip 文件,问题是百分比计算超过 100%,几乎达到 111%。这是代码:

    boolean UNZipFiles() {
    byte[] buffer = new byte[4096];
    int length;
    float prev = -1; // to check if the percent changed and its worth updating the UI
    int finalSize = 0;
    float current = 0;

    String zipFile = PATH + FileName;

    FileInputStream fin = new FileInputStream(zipFile);
    ZipInputStream zin = new ZipInputStream(fin);

    finalSize = (int) new File(zipFile).length();

    ZipEntry ze = null;

    while ((ze = zin.getNextEntry()) != null) {

        current += ze.getSize();

        if (ze.isDirectory())
            dirChecker(ze.getName());
        else {
            FileOutputStream fout = new FileOutputStream(PATH + ze.getName());
            while ((length = zin.read(buffer)) > 0)
                fout.write(buffer, 0, length);

            if (prev != current / finalSize * 100) {
                prev = current / finalSize * 100;
                UpdatePercentNotificationBar((int) prev);
            }
            zin.closeEntry();
            fout.close();
        }

    }

    zin.close();

    return true;
}

我该如何解决这个问题?

最佳答案

finalSize = (int) new File(zipFile).length(); 是压缩文件的大小,而 ze.getSize(); 返回大小未压缩的数据。

因此您的最终 % 将是:(未压缩数据的大小)/(zip 文件的大小)

使用 ze.getCompressedSize() 可能会获得更好的结果。

关于java - 解压缩 zip 文件超过 100% 时的百分比计算!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863970/

相关文章:

linux - 在顶部保留一个进度条。 Linux

java - 如何确定DataHandler.writeTo()方法当前写入的字节数?

java - 解析不同格式的日期的推荐方法是什么?

visual-c++ - 使用 boost 和 Visual C++ 2005 解压缩 zip 文件?

java - Google Analytics - 强制屏幕跟踪

java - 如何在保存到磁盘之前验证 ZipFile 的内容

python - AWS Lambda 压缩文件命令

java - 在保存数据的同时阻止对应用程序的访问的最佳实践?

java - 检查字符串是否由唯一字母组成的最简单方法?

java - Servlet 过滤器 - 更改 URL 不起作用