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;
// 獲取解壓之后文件的大小,用來計(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)度條功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(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)對話框形式進(jìn)度條功能示例
相關(guān)文章
Android內(nèi)存泄漏排查利器LeakCanary
這篇文章主要為大家詳細(xì)介紹了Android內(nèi)存泄漏排查利器LeakCanary的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android編程實(shí)現(xiàn)二維碼的生成與解析
這篇文章主要介紹了Android編程實(shí)現(xiàn)二維碼的生成與解析方法,結(jié)合實(shí)例分析了Android二維碼的生成與讀取二維碼的相關(guān)技巧,并提供了二維碼jar包供讀者下載,需要的朋友可以參考下2015-11-11
Android自定義View實(shí)現(xiàn)shape圖形繪制
這篇文章主要為大家詳細(xì)介紹了Android使用自定義View實(shí)現(xiàn)shape圖形繪制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)易新聞客戶端首頁效果的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android 防止過快(多次)點(diǎn)擊的實(shí)現(xiàn)方法
很多用戶經(jīng)常會(huì)出現(xiàn)過快且多次點(diǎn)擊同一按鈕的情況,本篇文章主要介紹了Android 防止過快點(diǎn)擊的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android運(yùn)動(dòng)健康睡眠自定義控件的實(shí)現(xiàn)
這篇文章主要介紹了Android實(shí)現(xiàn)運(yùn)動(dòng)健康睡眠自定義控件的方法,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-03-03
Android實(shí)現(xiàn)在xml文件中引用自定義View的方法分析
這篇文章主要介紹了Android實(shí)現(xiàn)在xml文件中引用自定義View的方法,結(jié)合實(shí)例形式分析了Android自定義view的實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
基于Android ContentProvider的總結(jié)詳解
本篇文章是對Android ContentProvider進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下2013-05-05

