首页 > 解决方案 > Android: Efficiently copy a zip containing thousands of files from assets to internal storage

问题描述

TL;DR: Is there a way to copy a zip file containing thousands of files from Android assets to internal storage that is more efficient than using ZipInputStream?

My Android app has several assets that need to be copied into device storage upon initial launch. These are stored as zip files in assets and are copied via ZipInputStream as described here (in the unzip method). There are 10 zip files totalling 36MB, and the unzip/copy process takes about 3 seconds.

The problem: I need to add a new asset that is 39MB, but it adds about 30 seconds to the process. My hunch is that this is because the asset consists of 5500 files (averaging about 7KB each). Since I need the assets at launch, running this in a background service is not an option, and 30+ seconds is a really long time to show a splash screen.

This post suggests using ZipFile instead of ZipInputStream but it does not seem to work properly in Android as noted here and other S/O posts, and I am experiencing the same ZipException described there (note this is after copying the zip file to internal storage - Android assets only provides a stream, not a file, so the zip must be copied from assets before the ZipFile method can be used).

Is there a more efficient way to go about this?

标签: androidunzipandroid-assets

解决方案


不幸的是 - 没有,因为写入每个文件都包含 3 个主要操作:创建和打开文件以进行写入,将数据写入文件,关闭文件以进行写入。复制这么多文件的最快方法 - 将它们放在一个文件中,例如二进制文件或 sqlite 数据库文件。或者您可以找到一种直接从存档中读取的方法。请记住,您将无法从资产中删除此文件(至少我从未听说过解决方案),所以它对我来说似乎没用。


推荐阅读