如何使用Android實現(xiàn)文件解壓帶進(jìn)度條功能

這篇文章給大家分享的是有關(guān)如何使用Android實現(xiàn)文件解壓帶進(jìn)度條功能的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站專注于網(wǎng)站建設(shè)|網(wǎng)站維護(hù)公司|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計與制作經(jīng)驗,為許多企業(yè)提供了網(wǎng)站定制設(shè)計服務(wù),案例作品覆蓋火鍋店設(shè)計等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身設(shè)計品質(zhì)網(wǎng)站。

解壓的工具類

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ì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) { 
    /** 因為會頻繁的刷新,這里我只是進(jìn)度>1%的時候才去顯示 */ 
    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)的.看個人需求,//注意了,因為解壓是放在線程中執(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() { 
      } 
    }); 
  } 
}

感謝各位的閱讀!關(guān)于“如何使用Android實現(xiàn)文件解壓帶進(jìn)度條功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

當(dāng)前文章:如何使用Android實現(xiàn)文件解壓帶進(jìn)度條功能
網(wǎng)站URL:http://muchs.cn/article4/ihcsie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、動態(tài)網(wǎng)站微信公眾號、網(wǎng)站設(shè)計公司網(wǎng)站收錄、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計