Matt Robertson
Matt Robertson

Reputation: 3165

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?

Upvotes: 1

Views: 161

Answers (1)

Arthur
Arthur

Reputation: 789

Unfortunately - no as writing each file consist of 3 main operations: create and open file for writing, write data in file, close file for writing. The fastest way to copy such amount of files - place them in one file, for example binary or file of sqlite database. Or you can find a way to read directly from archive. Keep in mind that you won't be able to delete this file from assets (at least I never heard of solution for that) so it seems useless to me.

Upvotes: 1

Related Questions