解壓的工具類
package com.example.videodemo.zip; public class ZipProgressUtil { /*** * 解壓通用方法 * * @param zipFileString * 文件路徑 * @param outPathString * 解壓路徑 * @param listener * 加壓監聽 */ public static void UnZipFile(final String zipFileString, final String outPathString, final ZipListener listener) { Thread zipThread = new UnZipMainThread(zipFileString, outPathString, listener); zipThread.start(); } public interface ZipListener { /** 開始解壓 */ void zipStart(); /** 解壓成功 */ void zipSuccess(); /** 解壓進度 */ void zipProgress(int progress); /** 解壓失敗 */ void zipFail(); } } 解壓線程
package com.example.videodemo.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import com.example.videodemo.zip.ZipProgressUtil.ZipListener; public class UnZipMainThread extends Thread { String zipFileString; String outPathString; ZipListener listener; public UnZipMainThread(String zipFileString, String outPathString, ZipListener listener) { this.zipFileString = zipFileString; this.outPathString = outPathString; this.listener = listener; } @Override public void run() { super.run(); try { listener.zipStart(); long sumLength = 0; // 獲取解壓之后文件的大小,用來計算解壓的進度 long ziplength = getZipTrueSize(zipFileString); System.out.println("====文件的大小==" + ziplength); FileInputStream inputStream = new FileInputStream(zipFileString); ZipInputStream inZip = new ZipInputStream(inputStream); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { szName = szName.substring(0, szName.length() - 1); File folder = new File(outPathString + File.separator + szName); folder.mkdirs(); } else { File file = new File(outPathString + File.separator + szName); file.createNewFile(); FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; while ((len = inZip.read(buffer)) != -1) { sumLength += len; int progress = (int) ((sumLength * 100) / ziplength); updateProgress(progress, listener); out.write(buffer, 0, len); out.flush(); } out.close(); } } listener.zipSuccess(); inZip.close(); } catch (Exception e) { listener.zipFail(); } } int lastProgress = 0; private void updateProgress(int progress, ZipListener listener2) { /** 因為會頻繁的刷新,這里我只是進度>1%的時候才去顯示 */ if (progress > lastProgress) { lastProgress = progress; listener2.zipProgress(progress); } } /** * 獲取壓縮包解壓后的內存大小 * * @param filePath * 文件路徑 * @return 返回內存long類型的值 */ 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; } } 界面調用方法.我使用的是靜態的方法,方便,可以改成非靜態的.看個人需求,//注意了,因為解壓是放在線程中執行的,所以界面刷新的話,需要使用handler來刷新界面調用還是比較方便的
注意 :調用的方法傳入的路徑:
1:是壓縮文件的全路徑 /storage/reeman/1234.zip
2:解壓文件的路徑(非全路徑) /storage/reeman/zip
package com.example.videodemo; import com.example.videodemo.zip.ZipProgressUtil; import com.example.videodemo.zip.ZipProgressUtil.ZipListener; import android.app.Activity; import android.os.Bundle; import android.widget.ProgressBar; public class MainActivity extends Activity { private ProgressBar progressBar1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar1 = (ProgressBar) findViewById(R.id.progressBar1); ZipProgressUtil.UnZipFile("解壓文件的路徑", "解壓之后的路徑", new ZipListener() { public void zipSuccess() { } public void zipStart() { } public void zipProgress(int progress) { } public void zipFail() { } }); } } 總結
以上所述是小編給大家介紹的Android實現文件解壓帶進度條功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!
新聞熱點
疑難解答