开发者

Unzipping files in a faster way than using the java.util.zip in Android

开发者 https://www.devze.com 2023-02-10 17:28 出处:网络
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

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 = n开发者_JS百科ew 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();

    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号