Android实现下载zip压缩文件并解压的方法(附源码)
程序员文章站
2023-12-16 10:34:58
前言
其实在网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,所以这篇文章在此记录一下下载zip文件并直接解压的方法,直接上代码,文末有源码下载。...
前言
其实在网上有很多介绍下载文件或者解压zip文件的文章,但是两者结合的不多,所以这篇文章在此记录一下下载zip文件并直接解压的方法,直接上代码,文末有源码下载。
下载:
import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.malformedurlexception; import java.net.url; import java.net.urlconnection; import android.app.progressdialog; import android.content.context; import android.content.dialoginterface; import android.content.dialoginterface.oncancellistener; import android.os.asynctask; import android.util.log; public class downloadertask extends asynctask<void, integer, long> { private final string tag = "downloadertask"; private url murl; private file mfile; private progressdialog mdialog; private int mprogress = 0; private progressreportingoutputstream moutputstream; private context mcontext; public downloadertask(string url,string out,context context){ super(); if(context!=null){ mdialog = new progressdialog(context); mcontext = context; } else{ mdialog = null; } try { murl = new url(url); string filename = new file(murl.getfile()).getname(); mfile = new file(out, filename); log.d(tag, "out="+out+", name="+filename+",murl.getfile()="+murl.getfile()); } catch (malformedurlexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override protected void onpreexecute() { // todo auto-generated method stub //super.onpreexecute(); if(mdialog!=null){ mdialog.settitle("downloading..."); mdialog.setmessage(mfile.getname()); mdialog.setprogressstyle(progressdialog.style_horizontal); mdialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface dialog) { // todo auto-generated method stub cancel(true); } }); mdialog.show(); } } @override protected long doinbackground(void... params) { // todo auto-generated method stub return download(); } @override protected void onprogressupdate(integer... values) { // todo auto-generated method stub //super.onprogressupdate(values); if(mdialog==null) return; if(values.length>1){ int contentlength = values[1]; if(contentlength==-1){ mdialog.setindeterminate(true); } else{ mdialog.setmax(contentlength); } } else{ mdialog.setprogress(values[0].intvalue()); } } @override protected void onpostexecute(long result) { // todo auto-generated method stub //super.onpostexecute(result); if(mdialog!=null&&mdialog.isshowing()){ mdialog.dismiss(); } if(iscancelled()) return; ((mainactivity)mcontext).showunzipdialog(); } private long download(){ urlconnection connection = null; int bytescopied = 0; try { connection = murl.openconnection(); int length = connection.getcontentlength(); if(mfile.exists()&&length == mfile.length()){ log.d(tag, "file "+mfile.getname()+" already exits!!"); return 0l; } moutputstream = new progressreportingoutputstream(mfile); publishprogress(0,length); bytescopied =copy(connection.getinputstream(),moutputstream); if(bytescopied!=length&&length!=-1){ log.e(tag, "download incomplete bytescopied="+bytescopied+", length"+length); } moutputstream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return bytescopied; } private int copy(inputstream input, outputstream output){ byte[] buffer = new byte[1024*8]; bufferedinputstream in = new bufferedinputstream(input, 1024*8); bufferedoutputstream out = new bufferedoutputstream(output, 1024*8); int count =0,n=0; try { while((n=in.read(buffer, 0, 1024*8))!=-1){ out.write(buffer, 0, n); count+=n; } out.flush(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ try { out.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { in.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return count; } private final class progressreportingoutputstream extends fileoutputstream{ public progressreportingoutputstream(file file) throws filenotfoundexception { super(file); // todo auto-generated constructor stub } @override public void write(byte[] buffer, int byteoffset, int bytecount) throws ioexception { // todo auto-generated method stub super.write(buffer, byteoffset, bytecount); mprogress += bytecount; publishprogress(mprogress); } } }
解压:
import java.io.bufferedinputstream; import java.io.bufferedoutputstream; import java.io.file; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.enumeration; import java.util.zip.zipentry; import java.util.zip.zipexception; import java.util.zip.zipfile; import android.app.progressdialog; import android.content.context; import android.content.dialoginterface; import android.content.dialoginterface.oncancellistener; import android.os.asynctask; import android.util.log; public class zipextractortask extends asynctask<void, integer, long> { private final string tag = "zipextractortask"; private final file minput; private final file moutput; private final progressdialog mdialog; private int mprogress = 0; private final context mcontext; private boolean mreplaceall; public zipextractortask(string in, string out, context context, boolean replaceall){ super(); minput = new file(in); moutput = new file(out); if(!moutput.exists()){ if(!moutput.mkdirs()){ log.e(tag, "failed to make directories:"+moutput.getabsolutepath()); } } if(context!=null){ mdialog = new progressdialog(context); } else{ mdialog = null; } mcontext = context; mreplaceall = replaceall; } @override protected long doinbackground(void... params) { // todo auto-generated method stub return unzip(); } @override protected void onpostexecute(long result) { // todo auto-generated method stub //super.onpostexecute(result); if(mdialog!=null&&mdialog.isshowing()){ mdialog.dismiss(); } if(iscancelled()) return; } @override protected void onpreexecute() { // todo auto-generated method stub //super.onpreexecute(); if(mdialog!=null){ mdialog.settitle("extracting"); mdialog.setmessage(minput.getname()); mdialog.setprogressstyle(progressdialog.style_horizontal); mdialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface dialog) { // todo auto-generated method stub cancel(true); } }); mdialog.show(); } } @override protected void onprogressupdate(integer... values) { // todo auto-generated method stub //super.onprogressupdate(values); if(mdialog==null) return; if(values.length>1){ int max=values[1]; mdialog.setmax(max); } else mdialog.setprogress(values[0].intvalue()); } private long unzip(){ long extractedsize = 0l; enumeration<zipentry> entries; zipfile zip = null; try { zip = new zipfile(minput); long uncompressedsize = getoriginalsize(zip); publishprogress(0, (int) uncompressedsize); entries = (enumeration<zipentry>) zip.entries(); while(entries.hasmoreelements()){ zipentry entry = entries.nextelement(); if(entry.isdirectory()){ continue; } file destination = new file(moutput, entry.getname()); if(!destination.getparentfile().exists()){ log.e(tag, "make="+destination.getparentfile().getabsolutepath()); destination.getparentfile().mkdirs(); } if(destination.exists()&&mcontext!=null&&!mreplaceall){ } progressreportingoutputstream outstream = new progressreportingoutputstream(destination); extractedsize+=copy(zip.getinputstream(entry),outstream); outstream.close(); } } catch (zipexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ try { zip.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return extractedsize; } private long getoriginalsize(zipfile file){ enumeration<zipentry> entries = (enumeration<zipentry>) file.entries(); long originalsize = 0l; while(entries.hasmoreelements()){ zipentry entry = entries.nextelement(); if(entry.getsize()>=0){ originalsize+=entry.getsize(); } } return originalsize; } private int copy(inputstream input, outputstream output){ byte[] buffer = new byte[1024*8]; bufferedinputstream in = new bufferedinputstream(input, 1024*8); bufferedoutputstream out = new bufferedoutputstream(output, 1024*8); int count =0,n=0; try { while((n=in.read(buffer, 0, 1024*8))!=-1){ out.write(buffer, 0, n); count+=n; } out.flush(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }finally{ try { out.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } try { in.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } return count; } private final class progressreportingoutputstream extends fileoutputstream{ public progressreportingoutputstream(file file) throws filenotfoundexception { super(file); // todo auto-generated constructor stub } @override public void write(byte[] buffer, int byteoffset, int bytecount) throws ioexception { // todo auto-generated method stub super.write(buffer, byteoffset, bytecount); mprogress += bytecount; publishprogress(mprogress); } } }
权限:
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <!-- 创建和删除文件 --> <uses-permission android:name="android.permission.mount_unmount_filesystems" /> <!-- 写文件 --> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.vibrate" /> <uses-permission android:name="android.permission.read_apn_settings" /> <uses-permission android:name="android.permission.restart_packages"/> <!-- 统计 --> <uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.read_phone_state" /> <uses-permission android:name="android.permission.read_logs" /> <uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.change_configuration" />
源码下载:点击这里
总结
以上就是这篇文章的全部内容了,希望这篇文章对各位android开发者们能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对能带来一定的帮助。