欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现文件解压带进度条功能

程序员文章站 2023-11-20 18:36:58
解压的工具类 package com.example.videodemo.zip; public class zipprogressutil { /*...

解压的工具类

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实现文件解压带进度条功能,希望对大家有所帮助