Pages

Wednesday, August 3, 2011

Extracting Zip File in Android

The code snippet below explains how to extract zip file in android
You can call extractZip method with activity from where its called and path of file to be extracted
It will extract the files on same destination path
/**
 * This method will extract the zip file from location inside application 
 * 
 * @param activity
 * @param destinationPath - Path of zip file
 * @param _location - location where file will be extracted
 * @author Yash@iotasol.com
 */
public void extractZip(Activity activity,String destinationPath,String _location ) {
System.out.println(" ctr : ::: extract zip" + zipExtracted + " : "
+ destinationPath);
FileInputStream fin = null;
ZipInputStream zin = null;
try {
fin = new FileInputStream(destinationPath);
zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
extractDirectoryZipEntry(ze.getName(), _location);
} else {
extractZipFileEntry(_location, zin, ze);
}
}
zipExtracted = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (zin != null) {
zin.close();
}
if (fin != null) {
fin.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}



/**
 * This method will work on every single zip entry and store it in location
 * Buffered input stream is used to speed up the downloading process
 * @param _location
 * @param zin
 * @param ze
 * @author Yash@iotasol.com
 */
private void extractZipFileEntry(String _location, ZipInputStream zin,
ZipEntry ze) {
System.out.println(">>>>> file : " + ze.getName());
FileOutputStream fout = null;
BufferedOutputStream bout = null;
try {
fout = new FileOutputStream(_location + ze.getName());
bout = new BufferedOutputStream(fout, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = zin.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
}
} catch (Exception r) {
r.printStackTrace();
} finally {
try {
zin.closeEntry();
if (bout != null)
bout.close();
if (fout != null)
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(">>>>> file downloaded: " + ze.getName());
}


/**
 * This method will work on every single zip entry in case if its a directory, and creates new
 * 
 * @param dir
 * @param _location
 * @author Yash@iotasol.com
 */
private void extractDirectoryZipEntry(String dir, String _location) {

File f = new File(_location + dir);

if (!f.isDirectory()) {
f.mkdirs();
}
}


I hope this post will be helpful. 

Please share your comments and and you can also contact me @ yash@iotasol.com or visit our site www.iotasol.com , www.iotadomains.com.

18 comments:

  1. i m ur first follower after u ...:P

    ReplyDelete
  2. @Arsh: Thanks for your comment.

    ReplyDelete
  3. awesome solution.........really helped me......was stuck on this for quite some time....
    Thanks Iotasol. You Rock :)

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. It Rocks Mate,Simplest Steps with simplest coding,Looking forward for more of them

    ReplyDelete
  6. Thanks for this post.. It helped me... Whats is ur twitter account?

    ReplyDelete
  7. Thanks varun, my twitter is @ceryash and @iotasol

    ReplyDelete
  8. After reading your post I understood that last week was with full of surprises and happiness for you. Congratz! Even though the website is work related, you can update small events in your life and share your happiness with us too.

    Data Science with Python training in chenni
    Data Science training in chennai
    Data science training in velachery
    Data science training in tambaram
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore

    ReplyDelete
  9. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    python online training
    python training in OMR
    python training in tambaram

    ReplyDelete
  10. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram
    DevOps online Training

    ReplyDelete
  11. I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.
    Blueprism online training

    Blue Prism Training in Pune

    ReplyDelete
  12. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.Well written article Thank You Sharing with Us pmp training Chennai | pmp training centers in Chennai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

    ReplyDelete
  13. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  14. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Data science Related information please visit our website best course for data science page!

    ReplyDelete