从 Android 资产文件夹中的 ZIP 文件中读取文件 [英] Reading files from a ZIP file in your Android assets folder

查看:57
本文介绍了从 Android 资产文件夹中的 ZIP 文件中读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ZipInputStream 从位于我的 Android 资产文件夹中的 ZIP 文件中读取文件:它可以工作,但它真的很慢,因为它必须使用 getNextEntry(),还有相当多的文件.

I'm reading files from a ZIP file that's located in my Android assets folder using ZipInputStream: it works, but it's really slow, as it has to read it sequentially using getNextEntry(), and there are quite a lot of files.

如果我将 ZIP 文件复制到 SD 卡上,使用 ZipFile.getEntry 时读取速度非常快,但我没有找到将 ZipFile 与资产文件!

If I copy the ZIP file onto the SD card, reading is really fast when using ZipFile.getEntry, but I didn't find a way to use ZipFile with the asset file!

有没有办法快速访问资产文件夹中的ZIP?还是我真的必须将 ZIP 复制到 SD 卡?

Is there any way to access the ZIP in the asset folder in a speedy way? Or do I really have to copy the ZIP to the SD card?

(顺便说一句,如果有人想知道我为什么要这样做:该应用程序大于 50 MB,因此为了在 Play 商店中获得它,我必须使用扩展 APK;但是,因为该应用程序也应该是放入亚马逊应用商店,我必须为此使用另一个版本,因为亚马逊不支持扩展 APK,自然......我认为在两个不同位置访问 ZIP 文件将是处理此问题的简单方法,但是唉……)

(BTW, in case anybody wonders why I'm doing this: the app is larger than 50 MB, so in order to get it in the Play Store I have to use Expansion APKs; however, as this app should also be put into the Amazon App Store, I have to use another version for this, as Amazon doesn't support Expansion APKs, naturally... I thought that accessing a ZIP file at two different locations would be an easy way to handle this, but alas...)

推荐答案

这对我有用:

private void loadzip(String folder, InputStream inputStream) throws IOException
{
    ZipInputStream zipIs = new ZipInputStream(inputStream); 
    ZipEntry ze = null;

            while ((ze = zipIs.getNextEntry()) != null) {

                FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());

                byte[] buffer = new byte[1024];
                int length = 0;

                while ((length = zipIs.read(buffer))>0) {
                fout.write(buffer, 0, length);
                }
                zipIs.closeEntry();
                fout.close();
            }
            zipIs.close();
}

这篇关于从 Android 资产文件夹中的 ZIP 文件中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆