阅读在你的Andr​​oid资源文件夹从ZIP文件的文件你的、文件、文件夹、资源

2023-09-04 06:59:56 作者:当凸遇上凹

我读,就从被设在我的Andr​​oid资产的文件夹中使用 ZipInputStream 一个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();
}