1

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?

Matt Robertson
  • 2,928
  • 5
  • 34
  • 62
  • If you need that much files in the file-system something is wrong with the code that uses them. Re-write that code so that it directly loads the files from within a ZIP file or the assets. – Robert Apr 26 '19 at 17:47
  • 1
    An Android APK file is a ZIP-style archive already. Perhaps the solution is to not put ZIP files in `assets/`, but to put the unZIPped contents of those ZIP files in `assets/`. Then, either perhaps you can use them *in situ*, or you can at least copy the bare minimum number of those assets during startup. – CommonsWare Apr 26 '19 at 18:10

1 Answers1

1

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.

Arthur
  • 769
  • 7
  • 17