安卓学习笔记---文件解压带进度条

转载博客地址:

http://blog.csdn.net/fkgjdkblxckvbxbgb/article/details/54096549?locationNum=8&fps=1


解压的工具类

[java]  view plain  copy
  1. package com.example.videodemo.zip;  
  2.   
  3. public class ZipProgressUtil {  
  4.   
  5.     /*** 
  6.      * 解压通用方法 
  7.      *  
  8.      * @param zipFileString 
  9.      *            文件路径 
  10.      * @param outPathString 
  11.      *            解压路径 
  12.      * @param listener 
  13.      *            加压监听 
  14.      */  
  15.     public static void UnZipFile(final String zipFileString, final String outPathString, final ZipListener listener) {  
  16.         Thread zipThread = new UnZipMainThread(zipFileString, outPathString, listener);  
  17.         zipThread.start();  
  18.     }  
  19.   
  20.     public interface ZipListener {  
  21.         /** 开始解压 */  
  22.         void zipStart();  
  23.   
  24.         /** 解压成功 */  
  25.         void zipSuccess();  
  26.   
  27.         /** 解压进度 */  
  28.         void zipProgress(int progress);  
  29.   
  30.         /** 解压失败 */  
  31.         void zipFail();  
  32.     }  
  33.   
  34. }  


解压线程

[java]  view plain  copy
  1. package com.example.videodemo.zip;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.Enumeration;  
  8. import java.util.zip.ZipEntry;  
  9. import java.util.zip.ZipFile;  
  10. import java.util.zip.ZipInputStream;  
  11.   
  12. import com.example.videodemo.zip.ZipProgressUtil.ZipListener;  
  13.   
  14. public class UnZipMainThread extends Thread {  
  15.   
  16.     String zipFileString;  
  17.     String outPathString;  
  18.     ZipListener listener;  
  19.   
  20.     public UnZipMainThread(String zipFileString, String outPathString, ZipListener listener) {  
  21.         this.zipFileString = zipFileString;  
  22.         this.outPathString = outPathString;  
  23.         this.listener = listener;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void run() {  
  28.         super.run();  
  29.         try {  
  30.             listener.zipStart();  
  31.             long sumLength = 0;  
  32.             // 获取解压之后文件的大小,用来计算解压的进度  
  33.             long ziplength = getZipTrueSize(zipFileString);  
  34.             System.out.println("====文件的大小==" + ziplength);  
  35.             FileInputStream inputStream = new FileInputStream(zipFileString);  
  36.             ZipInputStream inZip = new ZipInputStream(inputStream);  
  37.   
  38.             ZipEntry zipEntry;  
  39.             String szName = "";  
  40.             while ((zipEntry = inZip.getNextEntry()) != null) {  
  41.                 szName = zipEntry.getName();  
  42.                 if (zipEntry.isDirectory()) {  
  43.                     szName = szName.substring(0, szName.length() - 1);  
  44.                     File folder = new File(outPathString + File.separator + szName);  
  45.                     folder.mkdirs();  
  46.                 } else {  
  47.                     File file = new File(outPathString + File.separator + szName);  
  48.                     file.createNewFile();  
  49.                     FileOutputStream out = new FileOutputStream(file);  
  50.                     int len;  
  51.                     byte[] buffer = new byte[1024];  
  52.                     while ((len = inZip.read(buffer)) != -1) {  
  53.                         sumLength += len;  
  54.                         int progress = (int) ((sumLength * 100) / ziplength);  
  55.                         updateProgress(progress, listener);  
  56.                         out.write(buffer, 0, len);  
  57.                         out.flush();  
  58.                     }  
  59.                     out.close();  
  60.                 }  
  61.             }  
  62.             listener.zipSuccess();  
  63.             inZip.close();  
  64.         } catch (Exception e) {  
  65.             listener.zipFail();  
  66.         }  
  67.     }  
  68.   
  69.     int lastProgress = 0;  
  70.   
  71.     private void updateProgress(int progress, ZipListener listener2) {  
  72.         /** 因为会频繁的刷新,这里我只是进度>1%的时候才去显示 */  
  73.         if (progress > lastProgress) {  
  74.             lastProgress = progress;  
  75.             listener2.zipProgress(progress);  
  76.         }  
  77.     }  
  78.   
  79.     /** 
  80.      * 获取压缩包解压后的内存大小 
  81.      *  
  82.      * @param filePath 
  83.      *            文件路径 
  84.      * @return 返回内存long类型的值 
  85.      */  
  86.     public long getZipTrueSize(String filePath) {  
  87.         long size = 0;  
  88.         ZipFile f;  
  89.         try {  
  90.             f = new ZipFile(filePath);  
  91.             Enumeration<? extends ZipEntry> en = f.entries();  
  92.             while (en.hasMoreElements()) {  
  93.                 size += en.nextElement().getSize();  
  94.             }  
  95.         } catch (IOException e) {  
  96.             e.printStackTrace();  
  97.         }  
  98.         return size;  
  99.     }  
  100.   
  101. }  



界面调用方法.我使用的是静态的方法,方便,可以改成非静态的.看个人需求,

//注意了,因为解压是放在线程中执行的,所以界面刷新的话,需要使用handler来刷新界面

调用还是比较方便的


注意 :调用的方法传入的路径:

        1:是压缩文件的全路径   /storage/reeman/1234.zip

         2:解压文件的路径(非全路径)   /storage/reeman/zip

      


[java]  view plain  copy
  1. package com.example.videodemo;  
  2.   
  3. import com.example.videodemo.zip.ZipProgressUtil;  
  4. import com.example.videodemo.zip.ZipProgressUtil.ZipListener;  
  5.   
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.ProgressBar;  
  9.   
  10. public class MainActivity extends Activity {  
  11.   
  12.     private ProgressBar progressBar1;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);  
  20.         ZipProgressUtil.UnZipFile("解压文件的路径""解压之后的路径"new ZipListener() {  
  21.   
  22.             public void zipSuccess() {  
  23.   
  24.             }  
  25.   
  26.             public void zipStart() {  
  27.   
  28.             }  
  29.   
  30.             public void zipProgress(int progress) {  
  31.   
  32.             }  
  33.   
  34.             public void zipFail() {  
  35.   
  36.             }  
  37.         });  
  38.   
  39.     }  
  40. }  






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值