在Android中以比使用java.util.zip更快的方式解压文件

6

我需要在Android上解压一个2.5MB的.zip文件(包含1087个文件-*.html,*.css和*.db),我已经使用了java.util.zip,它可以正常工作,但我需要提高性能,解压过程持续1.10分钟,我需要缩短这个时间。

我已经按照一些建议来提高性能,例如:

  • 使用BufferedInputStream、FileOutputStream和BufferedOutputStream。
  • 分块读取zip文件:

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

有没有办法改进我的代码?我正在寻找第三方zip程序来进行编程,例如我尝试了7ZipJBinding,但似乎Android不支持它,因为我引用了sevenzipjbinding.jar和sevenzipjbinding-AllPlatforms.jar,但我收到了一个错误:“在sevenzipjbinding-AllPlatforms中检测到本地库”。在7zip主页上有MAC、Windows、Linux版本,但我没有看到任何关于Android的信息。

请问您能推荐其他库来在Android上解压文件吗?

这是我的全部代码:

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

    }
}

你应该对代码进行性能分析/计时,以查看时间花费在哪里。您可以使用7zip或Info-ZIP的解压缩作为参考,以了解合理实现的速度应该如何。 - typo.pl
1087个文件太多了,可能是问题所在吗?另外,您尝试使用更大的缓冲区了吗? - bigstones
好的,谢谢。我会尝试这些建议,并在之后发表评论。非常感谢。 - Jhon
作为测试,您还可以尝试解压缩一个大小相同且仅包含单个文件的文件。这将告诉您问题是否在于正在写入的小文件数量。 - dave.c
请检查一下我的这段代码,也许对你有用。https://dev59.com/1KXja4cB1Zd3GeqPNCpN#46218795 - Kamlesh
1个回答

0

我相信你可以找到一个解压文件的C或C++代码片段,并通过Android NDK运行它。话虽如此,我不确定你能获得多少性能提升。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接