android 文件解压带进度条 Android实现文件解压带进度条功能
紫色飞鱼儿 人气:0想了解Android实现文件解压带进度条功能的相关内容吗,紫色飞鱼儿在本文为您仔细讲解android 文件解压带进度条的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,文件解压进度条,下面大家一起来学习吧。
解压的工具类
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实现文件解压带进度条功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
加载全部内容