Tuesday, 23 December 2014

HB Blog 45: How To Download And Unzip File From Server In Android???

In this post, I will show how to download zip file from server into android device and then extract files from the downloaded zip file.

Android class "java.net.URLConnection" is used for downloading purpose and "java.util.zip" package is used for unzipping purpose.


I have added two separate buttons for two asynctasks for downloading and unzipping purpose separately. I first used URLConnection class to download zip file from url and used InputStream to write file into sdcard. Then, used ZipInputStream class for unzipping the same file from sdcard.

Refer the below link for complete sample code:-
Download Sample Code
Download Apk File
Have a look on few code snippets,

Async_download.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
  * This class is used to download zip file.
  * @author <b>Harshal Benake</b>
  *
  */
 public class Async_download extends AsyncTask<String, String, String> {
  Activity mActivity;
  private ProgressDialog mProgressDialog;
  public Async_download(Activity activity) {
   this.mActivity=activity;
  }
  @Override
  protected void onPreExecute() {
   super.onPreExecute();
   mProgressDialog = new ProgressDialog(mActivity);
   mProgressDialog.setMessage("Downloading file..");
   mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   mProgressDialog.setCancelable(false);
   mProgressDialog.show();
  }

  @Override
  protected String doInBackground(String... aurl) {
   int count;
  try {
  URL url = new URL(aurl[0]);
  URLConnection connection = url.openConnection();
  connection.connect();

  int lenghtOfFile = connection.getContentLength();
  
  InputStream input = new BufferedInputStream(url.openStream());
  OutputStream output = new FileOutputStream(Constant.SDCARD+Constant.FILENAME);

  byte data[] = new byte[1024];
  long total = 0;
  
   while ((count = input.read(data)) != -1) {
    total += count;
    publishProgress(""+(int)((total*100)/lenghtOfFile));
    output.write(data, 0, count);
   }

   output.flush();
   output.close();
   input.close();
  } catch (Exception e) {}
  return null;

  }
  protected void onProgressUpdate(String... progress) {
    mProgressDialog.setProgress(Integer.parseInt(progress[0]));
  }

  @Override
  protected void onPostExecute(String unused) {
   mProgressDialog.dismiss();
  }

Async_unzipping.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * This class is used to unzip zip file from sdcard.
 * @author <b>Harshal Benake</b>
 *
 */
public class Async_unzipping extends AsyncTask<String, String, String> {
 Activity mActivity;
 private ProgressDialog mProgressDialog;

 public Async_unzipping(Activity activity) {
  this.mActivity=activity;
 }
 
 @Override
 protected void onPreExecute() {
  super.onPreExecute();
  mProgressDialog = new ProgressDialog(mActivity);
  mProgressDialog.setMessage("Extracting file..");
  mProgressDialog.setCancelable(false);
  mProgressDialog.show();
 }
   
 @Override
 protected String doInBackground(String... params) {
   
     try  { 
       FileInputStream fin = new FileInputStream(Constant.SDCARD+Constant.FILENAME); 
       ZipInputStream zin = new ZipInputStream(fin); 
       ZipEntry ze = null; 
       while ((ze = zin.getNextEntry()) != null) {    
        
           FileOutputStream fout = new FileOutputStream(Constant.SDCARD + ze.getName()); 
           for (int c = zin.read(); c != -1; c = zin.read()) { 
             fout.write(c); 
           } 
  
           zin.closeEntry(); 
           fout.close(); 
         } 
          
       zin.close(); 
     } catch(Exception e) { 
     } 
  
   
  return null;
 } 
 
 @Override
 protected void onPostExecute(String unused) {
  mProgressDialog.dismiss();
 }

3 comments:

  1. Hi
    I am using your code and its working fine.
    Anyway to unzip password protected zip file?

    ReplyDelete
  2. Replies
    1. All my sample demos are here on github https://github.com/harshalbenake/

      Delete