Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能
解壓的工具類
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; // 獲取解壓之后文件的大小,用來(lái)計(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來(lái)刷新界面調(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)站的支持!
- Android自定義雙向進(jìn)度條的實(shí)現(xiàn)代碼
- Android編程自定義進(jìn)度條顏色的方法詳解
- Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼
- Android自定義View仿華為圓形加載進(jìn)度條
- Android進(jìn)度條控件progressbar使用方法詳解
- Android實(shí)現(xiàn)蝸牛進(jìn)度條效果
- android 中win10 使用uwp控件實(shí)現(xiàn)進(jìn)度條Marquez效果
- Android自定義圓形進(jìn)度條
- Android自定義View實(shí)現(xiàn)環(huán)形進(jìn)度條的思路與實(shí)例
- android自定義進(jìn)度條漸變色View的實(shí)例代碼
- Android編程實(shí)現(xiàn)對(duì)話框形式進(jìn)度條功能示例
相關(guān)文章
Android 使用Zbar實(shí)現(xiàn)掃一掃功能
這篇文章主要介紹了Android 使用Zbar實(shí)現(xiàn)掃一掃功能,本文用的是Zbar實(shí)現(xiàn)掃一掃,因?yàn)楦鶕?jù)本人對(duì)兩個(gè)庫(kù)的使用比較,發(fā)現(xiàn)Zbar解碼比Zxing速度要快,實(shí)現(xiàn)方式也簡(jiǎn)單,需要的朋友可以參考下2023-03-03Android ListView添加頭布局和腳布局實(shí)例詳解
這篇文章主要介紹了Android ListView添加頭布局和腳布局實(shí)例詳解的相關(guān)資料,大家看下效果是否是自己想要實(shí)現(xiàn)的效果,這里附了實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作
這篇文章主要介紹了使用newInstance()來(lái)實(shí)例化fragment并傳遞數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08在Android項(xiàng)目中使用AspectJ的方法
這篇文章主要介紹了在Android項(xiàng)目中使用AspectJ的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Android隨機(jī)給出加減乘除的四則運(yùn)算算術(shù)題
這篇文章主要為大家詳細(xì)介紹了Android隨機(jī)給出加減乘除的四則運(yùn)算算術(shù)題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android?Jetpack結(jié)構(gòu)運(yùn)用Compose實(shí)現(xiàn)微博長(zhǎng)按點(diǎn)贊彩虹效果
Compose在動(dòng)畫方面下足了功夫,提供了豐富的API。但也正由于API種類繁多,如果想一氣兒學(xué)下來(lái),最終可能會(huì)消化不良,導(dǎo)致似懂非懂。結(jié)合例子學(xué)習(xí)是一個(gè)不錯(cuò)的方法,本文就帶大家邊學(xué)邊做,通過(guò)實(shí)現(xiàn)一個(gè)微博長(zhǎng)按點(diǎn)贊的動(dòng)畫效果,學(xué)習(xí)了解Compose動(dòng)畫的常見思路和開發(fā)技巧2022-07-07Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析
這篇文章主要介紹了Android 媒體開發(fā)之MediaPlayer狀態(tài)機(jī)接口方法實(shí)例解析,需要的朋友可以參考下2017-08-08Android之ScrollView嵌套ListView和GridView沖突的解決方法
由于ListView,GridView本身都繼承于ScrollView,一旦在ScrollView中嵌套ScrollView,在ScrollView中嵌套使用ListView或者GridView,ListView只會(huì)顯示一行多一點(diǎn)。兩者進(jìn)行嵌套,即會(huì)發(fā)生沖突2013-09-09