Preface
In fact, there are many articles on the Internet that introduce downloading files or decompressing zip files, but not many of them are combined, so this article records the method of downloading zip files and decompressing them directly, directly uploading the code, and downloading the source code at the end of the article.
download:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; 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(()).getName(); mFile = new File(out, fileName); (TAG, "out="+out+", name="+fileName+",()="+()); } catch (MalformedURLException e) { // TODO Auto-generated catch block (); } } @Override protected void onPreExecute() { // TODO Auto-generated method stub //(); if(mDialog!=null){ ("Downloading..."); (()); (ProgressDialog.STYLE_HORIZONTAL); (new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub cancel(true); } }); (); } } @Override protected Long doInBackground(Void... params) { // TODO Auto-generated method stub return download(); } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub //(values); if(mDialog==null) return; if(>1){ int contentLength = values[1]; if(contentLength==-1){ (true); } else{ (contentLength); } } else{ (values[0].intValue()); } } @Override protected void onPostExecute(Long result) { // TODO Auto-generated method stub //(result); if(mDialog!=null&&()){ (); } if(isCancelled()) return; ((MainActivity)mContext).showUnzipDialog(); } private long download(){ URLConnection connection = null; int bytesCopied = 0; try { connection = (); int length = (); if(()&&length == ()){ (TAG, "file "+()+" already exits!!"); return 0l; } mOutputStream = new ProgressReportingOutputStream(mFile); publishProgress(0,length); bytesCopied =copy((),mOutputStream); if(bytesCopied!=length&&length!=-1){ (TAG, "Download incomplete bytesCopied="+bytesCopied+", length"+length); } (); } catch (IOException e) { // TODO Auto-generated catch block (); } 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=(buffer, 0, 1024*8))!=-1){ (buffer, 0, n); count+=n; } (); } catch (IOException e) { // TODO Auto-generated catch block (); }finally{ try { (); } catch (IOException e) { // TODO Auto-generated catch block (); } try { (); } catch (IOException e) { // TODO Auto-generated catch block (); } } 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 (buffer, byteOffset, byteCount); mProgress += byteCount; publishProgress(mProgress); } } }
Unzip:
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; 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(!()){ if(!()){ (TAG, "Failed to make directories:"+()); } } 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 //(result); if(mDialog!=null&&()){ (); } if(isCancelled()) return; } @Override protected void onPreExecute() { // TODO Auto-generated method stub //(); if(mDialog!=null){ ("Extracting"); (()); (ProgressDialog.STYLE_HORIZONTAL); (new OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // TODO Auto-generated method stub cancel(true); } }); (); } } @Override protected void onProgressUpdate(Integer... values) { // TODO Auto-generated method stub //(values); if(mDialog==null) return; if(>1){ int max=values[1]; (max); } else (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>) (); while(()){ ZipEntry entry = (); if(()){ continue; } File destination = new File(mOutput, ()); if(!().exists()){ (TAG, "make="+().getAbsolutePath()); ().mkdirs(); } if(()&&mContext!=null&&!mReplaceAll){ } ProgressReportingOutputStream outStream = new ProgressReportingOutputStream(destination); extractedSize+=copy((entry),outStream); (); } } catch (ZipException e) { // TODO Auto-generated catch block (); } catch (IOException e) { // TODO Auto-generated catch block (); }finally{ try { (); } catch (IOException e) { // TODO Auto-generated catch block (); } } return extractedSize; } private long getOriginalSize(ZipFile file){ Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) (); long originalSize = 0l; while(()){ ZipEntry entry = (); if(()>=0){ originalSize+=(); } } 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=(buffer, 0, 1024*8))!=-1){ (buffer, 0, n); count+=n; } (); } catch (IOException e) { // TODO Auto-generated catch block (); }finally{ try { (); } catch (IOException e) { // TODO Auto-generated catch block (); } try { (); } catch (IOException e) { // TODO Auto-generated catch block (); } } 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 (buffer, byteOffset, byteCount); mProgress += byteCount; publishProgress(mProgress); } } }
Permissions:
<uses-permission android:name="" /> <uses-permission android:name=".ACCESS_NETWORK_STATE" /> <!-- Create and delete files --> <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- Write a file --> <uses-permission android:name=".READ_EXTERNAL_STORAGE" /> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name=".READ_PHONE_STATE" /> <uses-permission android:name="" /> <uses-permission android:name=".READ_APN_SETTINGS" /> <uses-permission android:name=".RESTART_PACKAGES"/> <!-- statistics --> <uses-permission android:name=".ACCESS_WIFI_STATE" /> <uses-permission android:name=".ACCESS_NETWORK_STATE" /> <uses-permission android:name=".READ_PHONE_STATE" /> <uses-permission android:name=".READ_LOGS" /> <uses-permission android:name=".WAKE_LOCK" /> <uses-permission android:name=".CHANGE_CONFIGURATION" />
Source code download:Click here
Summarize
The above is the entire content of this article. I hope this article will be of some help to all Android developers. If you have any questions, you can leave a message to communicate. Thank you for your help.