亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

Android中實現下載和解壓zip文件功能代碼分享

2020-04-11 11:38:55
字體:
來源:轉載
供稿:網友

本文提供了2段Android代碼,實現了從Android客戶端下載ZIP文件并且實現ZIP文件的解壓功能,非常實用,有需要的Android開發者可以嘗試一下。

下載:

DownLoaderTask.java

復制代碼 代碼如下:

package com.johnny.testzipanddownload;
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);
  }

 }
}

解壓:

ZipExtractorTask .java

復制代碼 代碼如下:

package com.johnny.testzipanddownload;
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);
  }

 }
}

Main Activity

MainActivity.java

復制代碼 代碼如下:

package com.johnny.testzipanddownload;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
 private final String TAG="MainActivity";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.d(TAG, "Environment.getExternalStorageDirectory()="+Environment.getExternalStorageDirectory());
  Log.d(TAG, "getCacheDir().getAbsolutePath()="+getCacheDir().getAbsolutePath());

  showDownLoadDialog();
  //doZipExtractorWork();
  //doDownLoadWork();
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 private void showDownLoadDialog(){
  new AlertDialog.Builder(this).setTitle("確認")
  .setMessage("是否下載?")
  .setPositiveButton("是", new OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onClick 1 = "+which);
    doDownLoadWork();
   }
  })
  .setNegativeButton("否", new OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onClick 2 = "+which);
   }
  })
  .show();
 }

 public void showUnzipDialog(){
  new AlertDialog.Builder(this).setTitle("確認")
  .setMessage("是否解壓?")
  .setPositiveButton("是", new OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onClick 1 = "+which);
    doZipExtractorWork();
   }
  })
  .setNegativeButton("否", new OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onClick 2 = "+which);
   }
  })
  .show();
 }

 public void doZipExtractorWork(){
  //ZipExtractorTask task = new ZipExtractorTask("/storage/usb3/system.zip", "/storage/emulated/legacy/", this, true);
  ZipExtractorTask task = new ZipExtractorTask("/storage/emulated/legacy/testzip.zip", "/storage/emulated/legacy/", this, true);
  task.execute();
 }

 private void doDownLoadWork(){
  DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/testzip.zip", "/storage/emulated/legacy/", this);
  //DownLoaderTask task = new DownLoaderTask("http://192.168.9.155/johnny/test.h264", getCacheDir().getAbsolutePath()+"/", this);
  task.execute();
 }
}

以上就是Android實現zip文件下載和解壓功能,希望對你有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产成人一区二区| 欧美精品性视频| 国产成人+综合亚洲+天堂| 国产精品高精视频免费| 欧洲精品在线视频| 亚洲女在线观看| 岛国精品视频在线播放| 欧美成人国产va精品日本一级| 久久精品成人欧美大片| 色www亚洲国产张柏芝| 日韩高清av在线| 欧美电影电视剧在线观看| 不卡av在线播放| 亚洲成av人影院在线观看| 欧美性少妇18aaaa视频| 国a精品视频大全| 欧美在线一区二区三区四| 国模精品一区二区三区色天香| 国产裸体写真av一区二区| 按摩亚洲人久久| 国产精品91久久久| 日本免费一区二区三区视频观看| 亚洲精品av在线播放| 97视频在线观看免费高清完整版在线观看| 国产专区精品视频| 亚洲第一视频在线观看| 日本精品视频网站| 亚洲天堂av图片| 久久免费国产视频| 一区二区三区黄色| 欧美巨乳在线观看| 日韩av网址在线观看| 一区二区三区国产在线观看| 国产做受69高潮| 久久国产加勒比精品无码| 97久久久免费福利网址| 国产性猛交xxxx免费看久久| 成人黄色av网| 亚洲欧美日韩精品久久亚洲区| 国产精品第100页| 欧美中文字幕在线| 久久久久在线观看| 欧美另类交人妖| 欧美成人高清视频| 欧美激情极品视频| 欧美黄色片在线观看| 深夜福利一区二区| 91精品国产91久久久久久吃药| 日韩成人免费视频| 日韩欧美亚洲一二三区| 国产成人亚洲综合| 久久视频免费观看| 亚洲激情免费观看| 在线观看精品自拍私拍| 亚洲电影免费观看| 欧美激情亚洲激情| 在线电影av不卡网址| 亚洲国产一区二区三区在线观看| 国产精品美女在线观看| 亚洲福利在线视频| 96sao精品视频在线观看| 国产精品网红福利| 国产精品欧美日韩| 欧美激情视频免费观看| 一区二区三区四区精品| 国产精品一区专区欧美日韩| 亚洲国产毛片完整版| 国产精品黄色影片导航在线观看| 国产精品69久久| 国产精品久久久999| 中文字幕免费精品一区| 日本精品免费一区二区三区| 欧美日韩中文在线观看| 国内精品久久久久久中文字幕| 精品综合久久久久久97| 一区二区亚洲精品国产| 最近2019中文字幕大全第二页| 亚洲精品综合精品自拍| 91精品国产自产在线老师啪| 亚洲视频一区二区| 亚洲精品成人免费| 久久影视电视剧凤归四时歌| 久久久久久亚洲精品不卡| 中文字幕亚洲一区二区三区| 欧美日韩另类视频| 亚洲精品99999| 亚洲激情国产精品| 欧美国产中文字幕| 久久五月情影视| 国产女人精品视频| 91精品啪aⅴ在线观看国产| 久久久久久中文字幕| 日韩网站在线观看| 国产一区二区丝袜| 高清欧美性猛交| 国产精品激情av电影在线观看| 草民午夜欧美限制a级福利片| 国产日韩欧美在线观看| 91精品久久久久久久久久久久久| 中文字幕精品在线| 欧美激情精品久久久久久| 亚洲欧洲日产国产网站| 国产精品日韩在线播放| 日韩亚洲一区二区| 国产日韩精品视频| 亚洲男人天堂2023| 久久91精品国产91久久跳| 欧美超级免费视 在线| 亚洲人成绝费网站色www| 日本人成精品视频在线| 国产精品电影观看| 久久男人av资源网站| 在线电影av不卡网址| 欧美成人精品h版在线观看| 91av视频在线播放| 国产欧美久久一区二区| 日韩中文视频免费在线观看| 69久久夜色精品国产7777| 欧美性xxxxx极品| 最新国产精品亚洲| 国产成人精品在线视频| 在线看福利67194| 中文字幕一区电影| 久久亚洲成人精品| 亚洲一区二区三区视频播放| 欧美中文字幕视频| 91精品视频在线免费观看| 亚洲free性xxxx护士白浆| 日韩电影中文字幕在线观看| 欧美做爰性生交视频| 色悠悠国产精品| 国产日韩欧美在线播放| 欧美一级在线亚洲天堂| 国产精品www网站| 亚洲精品国产美女| 欧美国产日本高清在线| 国产91|九色| 国产精品午夜视频| 久久天天躁狠狠躁夜夜躁2014| 欧美午夜精品伦理| 国产精品视频区1| 欧美又大粗又爽又黄大片视频| 亚洲精品资源在线| 国产成人高潮免费观看精品| 日韩最新在线视频| 91日韩在线视频| 国产精品∨欧美精品v日韩精品| 欧美在线观看一区二区三区| 456国产精品| 91wwwcom在线观看| 一本大道香蕉久在线播放29| 久久综合久久美利坚合众国| 久久久久久久久久久久av| 欧美高清无遮挡| 亚洲精品日韩久久久| 好吊成人免视频| 国产欧美在线观看| 国产九九精品视频| 狠狠躁夜夜躁人人躁婷婷91| 日韩欧美国产网站| 日韩欧美国产一区二区| 国产成人小视频在线观看| 欧美日韩中国免费专区在线看| 欧美重口另类videos人妖|