6

I need unzip a .zip file of 2.5mb(1087 files - *.html, *.css and *.db) in android, i have used java.util.zip, it works fine, but i need improve the performance, the unzip process last 1.10 minutes, i need reduce this time. I have followed some recomendations for improve the performance, for example :

  • Use BufferedInputStream, FileOutputStream and BufferedOutputStream.
  • Read the zip in blocks :

byte data[] = new byte[2048]; while ((counter = bisMediaFile.read(data, 0, 2048)) != -1) { bosMediaFile.write(data, 0, counter); }

Is there any way to improve my code?. I was searching third party zip programs to use programatically, for example i tried the 7ZipJBinding, but it looks like android doesn't support this, because i referenced the sevenzipjbinding.jar and sevenzipjbinding-AllPlatforms.jar but i get an error : "Native Libraries Detected in sevenzipjbinding-AllPlatforms". At 7zip homepage there are versions for MAC, Windows, Linux, but i didn't see anything about android. Could you please recommend any other library to unzip files in android?

This is my all code :

public static void processZipFile(String strBinaryPath,String strExtractPath, String strDestinationDBPath) throws Exception
{
    ZipFile zipInFile = null;
    try
    {
        if (strExtractPath != null)
        {
            zipInFile = new ZipFile(strBinaryPath);
            for (Enumeration<? extends ZipEntry> entries = zipInFile.entries(); entries.hasMoreElements();)
            {
                ZipEntry zipMediaEntry = entries.nextElement();
                if (zipMediaEntry.isDirectory())
                {
                    File mediaDir = new File(String.format("%s\\%s", strExtractPath, zipMediaEntry.getName()));
                    mediaDir.mkdirs();
                }
                else
                {
                        BufferedInputStream bisMediaFile = null;
                        FileOutputStream fosMediaFile = null; 
                        BufferedOutputStream bosMediaFile = null;
                        try
                        {
                                String strFileName = String.format("%s\\%s", strExtractPath, zipMediaEntry.getName());
                                File uncompressDir = new File(strFileName).getParentFile();
                                uncompressDir.mkdirs();

                                //if is a database file, extract to other path : android.movinginteractive.com/databases
                                if(strFileName.contains(".db"))
                                    strFileName = String.format("%s\\%s", strDestinationDBPath, ExtractDBName(zipMediaEntry.getName()));

                                bisMediaFile = new BufferedInputStream(zipInFile.getInputStream(zipMediaEntry));
                                fosMediaFile = new FileOutputStream(strFileName);
                                bosMediaFile = new BufferedOutputStream(fosMediaFile);

                                int counter;
                                byte data[] = new byte[2048];

                                while ((counter = bisMediaFile.read(data, 0, 2048)) != -1) 
                                {
                                    bosMediaFile.write(data, 0, counter);
                                }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            if (bosMediaFile != null)
                            {
                                bosMediaFile.flush();
                                bosMediaFile.close();
                            }
                            if (bisMediaFile != null)
                                bisMediaFile.close();
                        }
                }
            }


        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        if (zipInFile != null)
            zipInFile.close();
        File flZipToDelete = new File(strBinaryPath);
        if(flZipToDelete.exists())
            flZipToDelete.delete();

    }
}
Shaeldon
  • 873
  • 4
  • 18
  • 28
Jhon
  • 61
  • 1
  • 3
  • You should profile/time your code to see where the time is spent. You can use 7zip or Info-ZIP's unzip as a reference to see how fast a reasonable implementation should be done. – typo.pl Feb 16 '11 at 17:47
  • 1087 is a lot of files, could that be the problem? also, did you try with a bigger buffer? – bigstones Feb 16 '11 at 18:02
  • Ok Thanks, i am going to try the suggestions and after i will comment about this. Thank you very much – Jhon Feb 16 '11 at 18:29
  • As a test you could also try unzipping a file of the same size that contains only a single file. This would tell you if the problem is the number of small files being written. – dave.c Feb 16 '11 at 19:36
  • Please check my this code may be it will useful for you. https://stackoverflow.com/a/46218795/3962677 – Kamlesh Jun 27 '18 at 06:26

1 Answers1

0

I'm sure you could find a C or C++ code snippet for unzipping files and run it through the Android NDK. That said, I'm not sure what performance gains you might get.

Bryan Stern
  • 165
  • 2
  • 9