Android实现文件解压带进度条功能
解压的工具类
packagecom.example.videodemo.zip; publicclassZipProgressUtil{ /*** *解压通用方法 * *@paramzipFileString *文件路径 *@paramoutPathString *解压路径 *@paramlistener *加压监听 */ publicstaticvoidUnZipFile(finalStringzipFileString,finalStringoutPathString,finalZipListenerlistener){ ThreadzipThread=newUnZipMainThread(zipFileString,outPathString,listener); zipThread.start(); } publicinterfaceZipListener{ /**开始解压*/ voidzipStart(); /**解压成功*/ voidzipSuccess(); /**解压进度*/ voidzipProgress(intprogress); /**解压失败*/ voidzipFail(); } }
解压线程
packagecom.example.videodemo.zip; importjava.io.File; importjava.io.FileInputStream; importjava.io.FileOutputStream; importjava.io.IOException; importjava.util.Enumeration; importjava.util.zip.ZipEntry; importjava.util.zip.ZipFile; importjava.util.zip.ZipInputStream; importcom.example.videodemo.zip.ZipProgressUtil.ZipListener; publicclassUnZipMainThreadextendsThread{ StringzipFileString; StringoutPathString; ZipListenerlistener; publicUnZipMainThread(StringzipFileString,StringoutPathString,ZipListenerlistener){ this.zipFileString=zipFileString; this.outPathString=outPathString; this.listener=listener; } @Override publicvoidrun(){ super.run(); try{ listener.zipStart(); longsumLength=0; //获取解压之后文件的大小,用来计算解压的进度 longziplength=getZipTrueSize(zipFileString); System.out.println("====文件的大小=="+ziplength); FileInputStreaminputStream=newFileInputStream(zipFileString); ZipInputStreaminZip=newZipInputStream(inputStream); ZipEntryzipEntry; StringszName=""; while((zipEntry=inZip.getNextEntry())!=null){ szName=zipEntry.getName(); if(zipEntry.isDirectory()){ szName=szName.substring(0,szName.length()-1); Filefolder=newFile(outPathString+File.separator+szName); folder.mkdirs(); }else{ Filefile=newFile(outPathString+File.separator+szName); file.createNewFile(); FileOutputStreamout=newFileOutputStream(file); intlen; byte[]buffer=newbyte[1024]; while((len=inZip.read(buffer))!=-1){ sumLength+=len; intprogress=(int)((sumLength*100)/ziplength); updateProgress(progress,listener); out.write(buffer,0,len); out.flush(); } out.close(); } } listener.zipSuccess(); inZip.close(); }catch(Exceptione){ listener.zipFail(); } } intlastProgress=0; privatevoidupdateProgress(intprogress,ZipListenerlistener2){ /**因为会频繁的刷新,这里我只是进度>1%的时候才去显示*/ if(progress>lastProgress){ lastProgress=progress; listener2.zipProgress(progress); } } /** *获取压缩包解压后的内存大小 * *@paramfilePath *文件路径 *@return返回内存long类型的值 */ publiclonggetZipTrueSize(StringfilePath){ longsize=0; ZipFilef; try{ f=newZipFile(filePath); Enumerationen=f.entries(); while(en.hasMoreElements()){ size+=en.nextElement().getSize(); } }catch(IOExceptione){ e.printStackTrace(); } returnsize; } }
界面调用方法.我使用的是静态的方法,方便,可以改成非静态的.看个人需求,//注意了,因为解压是放在线程中执行的,所以界面刷新的话,需要使用handler来刷新界面调用还是比较方便的
注意:调用的方法传入的路径:
1:是压缩文件的全路径 /storage/reeman/1234.zip
2:解压文件的路径(非全路径) /storage/reeman/zip
packagecom.example.videodemo; importcom.example.videodemo.zip.ZipProgressUtil; importcom.example.videodemo.zip.ZipProgressUtil.ZipListener; importandroid.app.Activity; importandroid.os.Bundle; importandroid.widget.ProgressBar; publicclassMainActivityextendsActivity{ privateProgressBarprogressBar1; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar1=(ProgressBar)findViewById(R.id.progressBar1); ZipProgressUtil.UnZipFile("解压文件的路径","解压之后的路径",newZipListener(){ publicvoidzipSuccess(){ } publicvoidzipStart(){ } publicvoidzipProgress(intprogress){ } publicvoidzipFail(){ } }); } }
总结
以上所述是小编给大家介绍的Android实现文件解压带进度条功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!