比在 Android 中使用 java.util.zip 更快地解压缩文件

发布于 2024-10-17 17:17:17 字数 3798 浏览 2 评论 0原文

我需要在android中解压一个2.5mb的.zip文件(1087个文件 - *.html,*.css和*.db),我使用了java.util.zip,它工作正常,但我需要提高性能,解压过程持续 1.10 分钟,我需要减少这个时间。 我遵循了一些提高性能的建议,例如:

  • 使用 BufferedInputStream、FileOutputStream 和 BufferedOutputStream。
  • 以块为单位读取 zip:

byte data[] = new byte[2048]; while ((计数器 = bisMediaFile.read(数据, 0, 2048)) != -1) { bosMediaFile.write(数据, 0, 计数器); ?

有什么方法可以改进我的代码吗 我正在搜索第三方 zip 程序以编程方式使用,例如我尝试了 7ZipJBinding,但看起来 Android 不支持此功能,因为我引用了 SevenzipjBinding.jar 和 SevenZipJBinding-AllPlatforms.jar 但我收到错误:“Native在 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();

    }
}

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

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2024-10-24 17:17:17

我确信您可以找到用于解压缩文件的 C 或 C++ 代码片段并通过 Android NDK 运行它。也就是说,我不确定您可能会获得什么性能提升。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文