I’m extracting a zip file and the problem is that the percentage calculation goes over 100% and reaches almost to 111%.. Here is the code:
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;
}
How can I fix this?
finalSize = (int) new File(zipFile).length();is the size of the compressed file, whereasze.getSize();returns the size of the uncompressed data.So your final % will be: (size of the uncompressed data) / (size of the zip file)
You would probably get a better result with
ze.getCompressedSize().