1

私は 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;
}

どうすればこれを修正できますか?

4

4 に答える 4

3

finalSize = (int) new File(zipFile).length();は圧縮ファイルのze.getSize();サイズで、圧縮されていないデータのサイズを返します。

したがって、最終的な % は次のようになります: (圧縮されていないデータのサイズ) / (zip ファイルのサイズ)

を使用すると、おそらくより良い結果が得られze.getCompressedSize()ます。

于 2012-08-08T11:57:59.203 に答える
3

パーセンテージを計算するには、zip ファイルの読み取り中にバイト数をカウントする必要があります...

于 2012-08-08T12:01:54.393 に答える
2
finalSize = (int) new File(zipFile).length();

これは、展開された zip ファイルのサイズではなく、zip ファイル自体のサイズを示します。

于 2012-08-08T11:58:08.733 に答える
1

は、そのZipEntry.getSize()エントリの非圧縮サイズを返します。試してみてくださいZipEntry.getCompressedSize()

于 2012-08-08T12:02:13.293 に答える