Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unzip large zip file & write to SDCARD in less time

To unzip zip file I've used classes from package java.util.zip* by refering this and it works correctly however to unzip a file of 40MB it takes 59 seconds. When I tried same zip file on iPhone project (we are developing app for both platforms - Android & iPone & that have functionality to unzip zip file & save unzipped content to SDCARD-Android or document directory - iPhone ), it takes only 14 seconds. The iphone app uses ziparchive.

So my question are:

1.From above experiment it clears that unzipping & file write operation to SDCARD in Java consumes more time as compared to iPhone app, so I decided to use C/C++ level unzipping & file write operation using NDK. Is this right choice ?

2.I have searched on google, stackoverflow & some suggested to use minizip but there no sufficient help on how to use minizip in android. Is anyboday have tried minizip for android ?

3.I also tried NDK development for libz to achieve my goal, as Libz is added in NDK but not getting how to use it. Is anybody tried libz in NDK?

4.Is there any other Framework in Java or C/C++ that unzip large zip file & write them to SDCARD in less time ?

Please help me.

Here is my Java Unzip Code :

public String unzip() {

    String result;

    try {

        FileInputStream fin = new FileInputStream(this.filePath);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;

        while ((ze = zin.getNextEntry()) != null) {

            Log.v("Unzip", "Unzipping " + ze.getName());

            if (ze.isDirectory()) {
                _dirChecker(ze.getName());
            } else {

                // Read 16 k at a time 
                byte[] buffer = new byte[16*1024];
                 int read;

                FileOutputStream fout = new FileOutputStream(this.location+ "/" + ze.getName());

                while ((read = zin.read(buffer)) != -1)
                {
                    fout.write(buffer, 0, read);
                }


                zin.closeEntry();
                fout.close();
            }

        }

        zin.close();
        result = "success";

    } catch (Exception e) {
        Log.e("unzip", "unzip", e);
        result = "failure";
    }

    return result;
}
like image 636
sachin003 Avatar asked Jan 08 '13 15:01

sachin003


People also ask

How do I open a zip file larger than 4GB?

If any single file in your zip file is over 4GB, then a 64-bit unarchiving program is required to open the . zip file, otherwise you will get a loop and be unable to extract the files. Learn more about 32-bit vs 64-bit.

How do I unzip a zip file with too long names?

Method 1 – Try using 7 zip when you use Explorer or WinZip to extract files it may throw an error which says a file path that exceeds the maximum character limit. To resolve this issue, you may use a decompression utility such as 7-Zip, that can handle long file paths.


2 Answers

Why don't you try this code.It works awesome

    String zipname = "data.zip";
FileInputStream fis = new FileInputStream(zipname);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;

while ((entry = zis.getNextEntry()) != null) {
  System.out.println("Unzipping: " + entry.getName());

  int size;
  byte[] buffer = new byte[2048];

  FileOutputStream fos = new FileOutputStream(entry.getName());
  BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

  while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
    bos.write(buffer, 0, size);
  }
  bos.flush();
  bos.close();
}
zis.close();
fis.close();

}

like image 133
Zubair Avatar answered Sep 28 '22 07:09

Zubair


All of the unzip code eventually ends up in zlib. There is no Java implementation of "deflate" compression in Android core libs.

The only reason java.util.Zip should be slower than a "native" unzip is if the file I/O is done badly, e.g. something is using really small buffers. Looking at the code linked from the question, this is exactly what's happening -- it's operating on individual bytes.

One of the comments on the solution provided a patch that uses a 4K buffer. Drop that in and see what happens to your performance.

like image 35
fadden Avatar answered Sep 28 '22 07:09

fadden