2012-08-08 71 views
1

我解壓一個zip文件和問題是百分比計算越過100%,幾乎達到111%。這裏是代碼:提取超過100%的zip文件時的百分比計算!

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

我該如何解決這個問題?

+0

解壓數據需要大量的CPU。當你解壓縮文件時,你期望發生什麼? – 2012-08-08 12:07:58

回答

2

finalSize = (int) new File(zipFile).length();是壓縮文件的大小,而ze.getSize();返回未壓縮數據的大小。

所以最終%將是:(未壓縮數據的大小)/(zip文件的大小)

你可能會得到一個更好的結果,ze.getCompressedSize()

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

這不會給你擴展zip文件的大小,它會給你zip文件本身的大小。

3

你在閱讀的zip文件,以計算百分比來算的字節數...

1

ZipEntry.getSize()返回該條目的未壓縮的大小。嘗試ZipEntry.getCompressedSize()