最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能

 更新時(shí)間:2017年08月09日 11:26:16   作者:紫色飛魚兒  
本文通過實(shí)例代碼給大家介紹了android實(shí)現(xiàn)文件解壓帶進(jìn)度條效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

解壓的工具類

package com.example.videodemo.zip; 
public class ZipProgressUtil { 
  /*** 
   * 解壓通用方法 
   * 
   * @param zipFileString 
   *      文件路徑 
   * @param outPathString 
   *      解壓路徑 
   * @param listener 
   *      加壓監(jiān)聽 
   */ 
  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(); 
    /** 解壓進(jìn)度 */ 
    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; 
      // 獲取解壓之后文件的大小,用來計(jì)算解壓的進(jìn)度 
      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) { 
    /** 因?yàn)闀?huì)頻繁的刷新,這里我只是進(jìn)度>1%的時(shí)候才去顯示 */ 
    if (progress > lastProgress) { 
      lastProgress = progress; 
      listener2.zipProgress(progress); 
    } 
  } 
  /** 
   * 獲取壓縮包解壓后的內(nèi)存大小 
   * 
   * @param filePath 
   *      文件路徑 
   * @return 返回內(nèi)存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; 
  } 
} 

界面調(diào)用方法.我使用的是靜態(tài)的方法,方便,可以改成非靜態(tài)的.看個(gè)人需求,//注意了,因?yàn)榻鈮菏欠旁诰€程中執(zhí)行的,所以界面刷新的話,需要使用handler來刷新界面調(diào)用還是比較方便的

注意 :調(diào)用的方法傳入的路徑:

        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() { 
      } 
    }); 
  } 
} 

總結(jié)

以上所述是小編給大家介紹的Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

富裕县| 白城市| 苗栗县| 冷水江市| 祁阳县| 林口县| 新晃| 汝城县| 康马县| 邹平县| 香河县| 嘉禾县| 丰城市| 遵义县| 崇义县| 安平县| 横山县| 容城县| 凤凰县| 富宁县| 崇明县| 都昌县| 内江市| 赤峰市| 丰顺县| 东辽县| 金山区| 文安县| 洪雅县| 迭部县| 巧家县| 会东县| 葫芦岛市| 荆门市| 凤冈县| 容城县| 辰溪县| 武穴市| 卢龙县| 施甸县| 太仓市|