国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現文件解壓帶進度條功能

2019-10-22 18:30:22
字體:
來源:轉載
供稿:網友

解壓的工具類

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武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昆明市| 武山县| 东方市| 日照市| 兴城市| 双峰县| 那坡县| 哈巴河县| 嘉祥县| 华蓥市| 尼木县| 宜兰市| 西乌| 忻州市| 哈巴河县| 栖霞市| 伊川县| 佛冈县| 陆川县| 曲靖市| 手游| 南丰县| 富阳市| 民勤县| 赫章县| 竹山县| 安溪县| 景东| 乌苏市| 专栏| 沽源县| 达日县| 小金县| 忻州市| 五家渠市| 宜城市| 汶上县| 阿合奇县| 玉龙| 平泉县| 苍梧县|