Android实现文件解压带进度条功能
作者:紫色飞鱼儿 时间:2023-02-26 19:01:05
解压的工具类
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实现文件解压带进度条功能网站的支持!
来源:http://blog.csdn.net/juhua2012/article/details/76964718
标签:android,文件解压,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
android中webview控件和javascript交互实例
2023-09-06 14:09:28
Mybatis控制台打印SQL语句的两种方式实现
2023-06-23 14:24:57
浅谈JVM垃圾回收之哪些对象可以被回收
2021-08-14 18:57:47
![](https://img.aspxhome.com/file/2023/5/61385_0s.png)
Android编程实现下载图片及在手机中展示的方法
2023-10-26 12:30:59
![](https://img.aspxhome.com/file/2023/8/139158_0s.png)
浅谈一下SpringCloud中Hystrix服务熔断和降级原理
2021-10-02 08:46:41
![](https://img.aspxhome.com/file/2023/6/129196_0s.jpg)
springboot2.0如何通过fastdfs实现文件分布式上传
2022-03-20 16:49:24
![](https://img.aspxhome.com/file/2023/7/71917_0s.png)
Android中socket通信的简单实现
2023-01-21 12:24:22
![](https://img.aspxhome.com/file/2023/2/138802_0s.jpg)
ShardingSphere解析SQL示例详解
2023-11-23 13:57:55
揭秘在ListView等AdapterView上动态添加删除项的陷阱
2022-11-26 16:02:27
![](https://img.aspxhome.com/file/2023/9/137969_0s.jpg)
Java学习关于循环和数组练习题整理
2022-07-19 21:01:24
SpringBoot+WebSocket实现即时通讯的方法详解
2021-07-24 15:48:49
![](https://img.aspxhome.com/file/2023/6/78866_0s.png)
Java实现FTP批量大文件上传下载篇2
2021-07-22 02:30:31
![](https://img.aspxhome.com/file/2023/3/83123_0s.jpg)
详解Java编程的Observer观察者设计模式
2023-11-10 04:06:07
Java数据结构之树和二叉树的相关资料
2022-07-31 04:48:18
![](https://img.aspxhome.com/file/2023/4/110664_0s.png)
Android 使用Picasso加载网络图片等比例缩放的实现方法
2023-08-29 15:36:32
java自定义注解验证手机格式的实现示例
2023-06-24 10:42:44
解析libcurl在android下的移植、编译与测试
2023-01-24 01:19:44
Android开发之android_gps定位服务简单实现
2023-07-31 20:02:25
![](https://img.aspxhome.com/file/2023/3/83553_0s.gif)
Spring main方法中如何调用Dao层和Service层的方法
2023-11-28 23:15:19
![](https://img.aspxhome.com/file/2023/3/60753_0s.png)
Java项目中获取路径的绝对路径问题和相对路径问题
2023-07-09 13:25:01
![](https://img.aspxhome.com/file/2023/2/59902_0s.jpg)