这篇文章给大家分享的是有关如何使用Android实现文件解压带进度条功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
解压的工具类
package com.example.videodemo.zip; public class ZipProgressUtil { 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) { if (progress > lastProgress) { lastProgress = progress; listener2.zipProgress(progress); } } 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实现文件解压带进度条功能”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!