Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Unzipping Files with Android (Programmatically)

I wrote an article a few months ago, which walked you through the process of zipping files programmatically with Java - in an Android environment. Due to popular demand, I've decided to write up another article on how to unzip files.

The process is pretty similar, and you could probably combine them both in a single class, but for this example, I made a class called Decompress.


import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 *
 * @author jon
 */
public class Decompress {
  private String _zipFile;
  private String _location;

  public Decompress(String zipFile, String location) {
    _zipFile = zipFile;
    _location = location;

    _dirChecker("");
  }

  public void unzip() {
    try  {
      FileInputStream fin = new FileInputStream(_zipFile);
      ZipInputStream zin = new ZipInputStream(fin);
      ZipEntry ze = null;
      while ((ze = zin.getNextEntry()) != null) {
        Log.v("Decompress", "Unzipping " + ze.getName());

        if(ze.isDirectory()) {
          _dirChecker(ze.getName());
        } else {
          FileOutputStream fout = new FileOutputStream(_location + ze.getName());
          for (int c = zin.read(); c != -1; c = zin.read()) {
            fout.write(c);
          }

          zin.closeEntry();
          fout.close();
        }
        
      }
      zin.close();
    } catch(Exception e) {
      Log.e("Decompress", "unzip", e);
    }

  }

  private void _dirChecker(String dir) {
    File f = new File(_location + dir);

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

Well, that's the class - tested on my Nexus One and works!

Here's an example of how to use it:


String zipFile = Environment.getExternalStorageDirectory() + "/files.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/";

Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();

That should unzip files.zip and put the contents into /sdcard/unzipped/, as was specified.

Check out my other article, if you want to find out how to zip files - Zipping Files with Android (Programmatically)


jon | October 27, 2010 | Comments (47)

Comments

http://www.howtoreducefatfromface.net
Comment by Deloras - August 16, 2014 @ 9:17 pm
http://www.howtoreducepimplestoday.com
Comment by Arlene - August 14, 2014 @ 4:10 pm
http://tibari.de/index.php?section=gallery&cid=7&pId=186
Comment by Jorge - August 09, 2014 @ 10:02 am
Thanks a lot Jon
Comment by Ashish - August 09, 2014 @ 4:22 am
http://makeupbaby.com/profile/vileech
Comment by Theda - July 16, 2014 @ 1:16 am
http://www.minirobo.ru/content/getting-back-ex-after-very-long-time
Comment by Leilani - July 11, 2014 @ 6:13 am
https://www.rebelmouse.com/sonia42tkeck/a-few-unique-guidelines-restau-614339333.html
Comment by Lindsay - July 08, 2014 @ 4:40 am
http://www.heateddogbed.org/
Comment by Jannette - July 06, 2014 @ 10:59 pm
http://www.wpseotricks.com/category/seo/
Comment by Allison - July 05, 2014 @ 8:57 am
http://webdesign-boeger.de/archive/48651
Comment by Benito - June 30, 2014 @ 9:56 am
http://www3.rn.ac.th/moodle/user/view.php?id=69816&course=1
Comment by Gennie - June 30, 2014 @ 7:50 am
http://www.newswire.net/newsroom/news/00083623-traffic-elixir-review.html
Comment by Kandis - June 27, 2014 @ 1:35 pm
http://www.easydisplay.co.uk/easy-slidestand-xl-billboards.html
Comment by Hayley - June 24, 2014 @ 7:01 pm
http://jd.krabbe.ca/tractors721050
Comment by Otis - June 07, 2014 @ 8:17 am
http://www.anonpedia.net/wiki/Gamers_de_El_portal_Youtube:_material_novedoso
Comment by Olaf - June 06, 2014 @ 10:13 am
http://Adolphkkxl434.Beeplog.com/442366_4264166.htm
Comment by Kate - June 06, 2014 @ 6:50 am
http://www.hoevehetrondgors.nl/index.php/gastenboek
Comment by Pearl - June 01, 2014 @ 1:06 am
http://ysd.be/cellineacena267217
Comment by Wanda - June 01, 2014 @ 1:06 am
http://www.hei-flyers.org/wordpress/groups/binaural-beats-what-jim-taught-jonny/
Comment by Trudy - May 30, 2014 @ 9:40 pm
http://www.carloscevola.com/
Comment by Bradford - May 28, 2014 @ 8:48 am
http://cpaintx.com/xe/?document_srl=127465
Comment by Emilie - May 11, 2014 @ 9:55 am
http://www.jondev.net/captcha/422b041b16996e8391c5f3f7b22490f9.png
Comment by http://www.jondev.net/captcha/422b041b16996e8391c5f3f7b22490f9.png - March 19, 2014 @ 9:53 am
http://www.richardgray.net/html/modules.php?name=Your_Account&op=userinfo&username=DeweyHudd
Comment by Klara - February 06, 2014 @ 9:57 pm
Hello sir,
I need to know whether this function will overwrite the existing files/folder while unzipping or not?
Comment by M00N - December 08, 2013 @ 5:32 am
Hello..this CODES is for JAVA right? I would like to ask if this .JAVA file needs also to create a .XML file to VIEW the RESULT or i say the DIRECTORIES? or this CODES is already MAKING a DIRECTORY to BROWSE what to ZIP/UNZIP a FILE?..i'm asking because our THESIS/CAPSTONE2 is having a abstract of ZIP and UNZIP so we would like to use this much more.. please REPLY ..
Comment by Chad - September 03, 2013 @ 3:05 pm
(sorry! Typo in my email) Hey, I have a question. when you unzip a file normally, where do the files go to? What map? Because Ive downloaded this emulator which needed a bios file to make it run, I downloaded it but I cant find it anywhere on my phone.. Ive got the samsung galaxy S2.
Comment by rick - May 24, 2012 @ 6:46 am
Hey, I have a question. when you unzip a file normally, where do the files go to? What map? Because Ive downloaded this emulator which needed a bios file to make it run, I downloaded it but I cant find it anywhere on my phone.. Ive got the samsung galaxy S2.
Comment by rick - May 24, 2012 @ 6:45 am
is it possible to implement password field for extracting the zip file?
Comment by Fahid - May 07, 2012 @ 5:26 am
If its too slow, consider optimizing by buffering output -- like this:

http://stackoverflow.com/questions/3975847/extrakting-zip-to-sd-card-is-very-slow-how-can-i-optimize-performance/10312761#10312761
Comment by Andre - April 25, 2012 @ 5:26 am
This works great when using the buffer Andy suggested. I would like it to display a Toast / Alert Dialog when the operation is done. How can I achieve that? Anyone can help?
Comment by Mircea M - April 05, 2012 @ 5:23 am
Highly recommend using a byte buffer like Andy mentioned, regardless of what type of device you use. For me this change was the difference between 15minutes to unzip and
Comment by AndDev - March 08, 2012 @ 3:21 pm
i am going to give this a try later.
Comment by chris franko - September 01, 2011 @ 8:31 am
How do I notify the MainActivity is done unzipping?
Comment by bong - June 15, 2011 @ 2:40 pm
Hello,

just wanted to tell you how happy I'm that I found your tutorial about unzipping files. It helped me alot! Thanks and have a great day. With your help it got clear how to tackle the problem!
Comment by Thomas - June 10, 2011 @ 6:42 am
Any solution for folders that contain a "SPACE"
??
Other then that it works great thanks
Comment by mxride - May 16, 2011 @ 1:04 pm
Help appreciated.

I have a foler on SD Card containing a ZIP archive,
Have created a test project with the above Class as MAIN, However I cant seem to figure out where to implement

String zipFile = Environment.getExternalStorageDirectory() + "/files.zip";
String unzipLocation = Environment.getExternalStorageDirectory() + "/unzipped/";

Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip()

Any help is much appreciated..
Comment by mxride - May 12, 2011 @ 11:44 am
Thanks, Jon. Well done.
Comment by matt - February 28, 2011 @ 4:39 pm
This code works fine to me only when i create all destination folder before unzipping.
For ex:if files.zip contains[
folder_image -> a.png & b.png
folder_css ->x.css
file.txt ]
Iam getting Err like:" File Not found exception .../a.png " what is the problem?
Comment by vnshetty - February 26, 2011 @ 7:09 am
Really awesome code. I too experienced the nasty speed issues. I used Andy's snippet from the earlier comment and it runs at lightning speed now. You should glue that into an update to this code.
Comment by Ray Dios Haque - February 22, 2011 @ 9:33 pm
Hi,

I have a problem. When I unzip a file and if it contains any folder which has white spaces in its name then unziping stops there and does not go ahead.How to deal if we have any folder having white space in it?
Please help.

Thanks
Parvendra
Comment by Parvendra - February 16, 2011 @ 1:09 am
@Andy That's a good point! Thanks for the heads up :)
Comment by xil3 - January 18, 2011 @ 8:38 am
Hey, thanks for the tutorial, it's very informative.

Regarding the slow performance in the emulator, I've found that it goes much faster (a few seconds as opposed to a minute in the emulator) if you copy the bytes into a buffer instead of individually:

byte[] buffer = new byte[1024];
int length;
...
// replace for loop with:
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
Comment by Andy - January 14, 2011 @ 7:13 pm
Really helpful,,
Good Tutorial
Comment by Farhan - January 11, 2011 @ 6:10 am
The emulator will be a lot slower than any of the phones that you run it on - test it on a phone and see the difference. It's also dependant on your computer - I've seen the emulator be extremely slow on some.

I tested a 10mb file on mine and it went quick.
Comment by xil3 - December 25, 2010 @ 11:46 am
I am using the emulator to test. It is actually slow in the looping.
for (int c = zin.read(); c != -1; c = zin.read()){
Log.v("Decompress", "For Looping "+ ze.getName()); fout.write(c);
}
Comment by Chan Yao Kang - December 21, 2010 @ 1:27 am
What kind of phone are you testing it on?

Might be worth logging the process, to see where it's slowing down (Log.v("", "")).
Comment by xil3 - December 18, 2010 @ 3:56 pm
I have tested the class,I found that the unzip function is very slow performance. Is there anyway to improve it? Its takes few min to unzip 1 MB file.
Comment by Chan Yao Kang - December 17, 2010 @ 1:20 am

Name (required)
Email (will not be published) (required)
Website

captcha