public class ZipUtils {
public static final String TAG = "ZIP";
public ZipUtils() {
}
/**
* zip
*
* @param zipFileString ZIP
* @param outPathString
* @throws Exception
*/
public void UnZipFolder(String zipFileString, String outPathString,UnZipListener unZipListener) {
ZipInputStream inZip = null;
FileOutputStream out = null;
try {
inZip= new ZipInputStream(new FileInputStream(zipFileString));
ZipEntry zipEntry;
long sumTotalLength = 0;
// ,
long ziplength = getZipTrueSize(zipFileString);
System.out.println(":" + ziplength);
while ((zipEntry = inZip.getNextEntry()) != null) {
sumTotalLength = unZipFile(zipEntry, outPathString, inZip, out, sumTotalLength, ziplength, unZipListener);
}
inZip.close();
if(unZipListener!=null)
unZipListener.onSuccess();
} catch (Exception e) {
e.printStackTrace();
if(unZipListener!=null){
unZipListener.onError(e.getMessage());
}
try {
if(inZip!=null)
inZip.close();
} catch (Exception e1) {
e1.printStackTrace();
}
try {
if(out!=null)
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
/**
*
* @param zipEntry
* @param outPathString
* @param inZip zip
* @param out zip
* @param sumTotalLength
* @param ziplength zip
* @param unZipListener
* @throws IOException
*/
private long unZipFile(ZipEntry zipEntry, String outPathString, ZipInputStream inZip, FileOutputStream out,
long sumTotalLength, long ziplength, UnZipListener unZipListener) throws IOException {
System.out.println("\r");
System.out.println("\r");
System.out.println("\r");
String szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
//
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
// System.out.println(":" + outPathString + File.separator + szName);
File file = new File(outPathString + File.separator + szName);
if (!file.exists()) {
// Log.e(TAG, "Create the file:" + outPathString + File.separator + szName);
file.getParentFile().mkdirs();
file.createNewFile();
}
long fileSize = zipEntry.getSize();
//
out= new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
long sumLength = 0;
long lastCurProgress = 0;
//zip0Ui
boolean isUpdateProgress = false;
//
while ((len = inZip.read(buffer)) != -1) {
if (unZipListener != null) {
if (ziplength != 0) {
sumLength += len;
sumTotalLength += len;
int totalProgress = (int) ((sumTotalLength * 100) / ziplength);
int curProgress;
if (fileSize != 0) {
curProgress = (int) ((sumLength * 100) / fileSize);
System.out.println(":" + szName + " :" + len + " ,:" + curProgress + " ,:" + totalProgress + " ,:" + fileSize);
if (fileSize < 1024 * 100) {
if (curProgress > 10) {
unZipListener.onUnZipFileProgress(szName, curProgress, totalProgress);
}
// if (curProgress > lastCurProgress) { //UI
// lastCurProgress = curProgress;
//
// }
} else {
if (curProgress > lastCurProgress) { //UI
lastCurProgress = curProgress;
// unZipListener.onUnZipFileProgress(szName, curProgress, totalProgress);
}
}
} else {
curProgress = 100;
if (curProgress > lastCurProgress) { //UI
lastCurProgress = curProgress;
// unZipListener.onUnZipFileProgress(szName, curProgress, totalProgress);
}
}
} else {
if (!isUpdateProgress) {
unZipListener.onUnZipFileProgress(szName, 100, 100);
isUpdateProgress = true;
}
}
}
// 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
return sumTotalLength;
}
/**
* zip
* @param filePath
* @return
*/
public long getZipTrueSize(String filePath) {
long size = 0;
ZipFile f;
try {
f = new ZipFile(filePath);
Enumeration<? extends ZipEntry> en = f.entries();
while (en.hasMoreElements()) {
size += en.nextElement().getSize();
}
} catch (IOException e) {
e.printStackTrace();
}
return size;
}
public interface UnZipListener {
void onSuccess();
void onError(String reason);
void onUnZipFileProgress(String name, int progress, int totalProgress);
}
}
I have actually implemented it now. The progress of zip decompression shows, but when a small file is encountered, the progress update speed is too fast to see which file is decompressed clearly. I want to make the progress value update of the small file slower
.