How to unzip a large zip file and write to sdcard in less time

To unzip the zip file, I used the classes from the java.util.zip * package, referring to this , and it works correctly, however, to unzip a 40 MB file for 59 seconds. When I tried the same zip file in the iPhone project (we are developing an application for both platforms - Android and iPone and which have the ability to unzip the zip file and save the unpacked content to SDCARD-Android or the document directory - iPhone), it takes only 14 seconds. The iphone application uses ziparchive .

So my question is:

1. Compared to the experiment, it clears that unpacking and writing the file to the SDCARD in Java consumes more time than the iPhone application, so I decided to use the unlock at the C / C ++ level and the file write operation using the NDK. Is it correct?

2. I searched google, stackoverflow, and some suggested using minizip , but there is not enough help on how to use minizip in android. Is anyboday an attempt to minizip for android?

3. I also tried developing the NDK for libz to achieve my goal, since Libz has been added to the NDK but is not getting how to use it. Has anyone tried libz in the NDK?

4. Is there any other Framework in Java or C / C ++ that decompresses a large ZIP file and writes them to SDCARD in less time?

Please help me.

Here is my Unzip code for Java:

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; } 
+7
source share
4 answers

Why don't you try this code. He works amazingly

  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(); 

}

+5
source

All unpacked codes end in zlib. In Java versions of Linux, there is no Java implementation of sleeping compression.

The only reason java.util.Zip should be slower than "native" unzip if the I / O file is not performing well, for example. something uses really small buffers. If you look at the code associated with the question, this is exactly what happens - it works with individual bytes.

One of the comments on the solution was provided by a patch that uses a 4K buffer. Take it off and see what happens to your performance.

+4
source

Try writing a 40 MB file to SDCard and measure the time spent. (Almost) all free (or even paid) implementations of zip archives support libraries are based on the same zlib code, which takes up most of the processing speed when unpacking. Java code should be much slower than the source, so I suggest trying to unlock NDK. In addition, an attempt to unzip an archive with a zero compression level will give you an idea of ​​how long the unzipping code takes and how much time is spent simply copying the data.

+1
source
 public boolean unzip(String zipfilepath, String destinationdir) { try { FileInputStream fis = new FileInputStream(zipfilepath); 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(destinationdir+ "/" + 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(); return true; }catch(Exception e){ return false; } } String zipfilepath = context.getFilesDir().getPath()+"/"+myfile.zip; String destinationdir = context.getFilesDir().getPath(); unzip(zipfilepath, destinationdir); 
0
source

All Articles