Search code examples
javaandroidkeraszipdl4j

Android: ZipFile() java.util.zip.ZipException: File too short to be a zip file: 0


I am working on a project (Using Android Studio 3.1.4) that reads in a neural network that is stored as a .zip file, for further use with DL4J in Android.

I am trying to open this .zip file located in my projects res\raw directory. For this I am trying to use the ZipFile() method from java.util.zip.

Screenshot of location of neuralnet.zip

Problem:

Following code throws an exception:

File model_file = new File(String.valueOf(this.getResources().openRawResource(R.raw.neuralnet)));
ZipFile zipFile = new ZipFile(model_file);

Exception:

"java.util.zip.ZipException: File too short to be a zip file: 0"

Therefore I cannot load the model. Testing this on a simulated device running Android API 24

On API 26 the exception is different:

java.io.FileNotFoundException: File doesn't exist: android.content.res.AssetManager$AssetInputStream@ddde727

Does someone have experience with the use of ZipFile(), or with loading neuralnetwork models using DL4J in Android?

Could there be something special be required in the build.gradle?

Any input is appreciated!

What I have tried:

  • Checked the .zip file made sure it exists.
  • Made sure that .zip is just a container, and the file is not compressed, by archiving the containing 3 files using 7Zip without compression as .zip
  • Checked READ_EXTERNAL_STORAGE permission, even though I dont think it should be required
  • tried using the full path instead of using this.getResources().openRawResource(R.raw.neuralnet)

Android permissions: I am running android api > 23, and I am requesting the READ_EXTERNAL_STORAGE permission at runtime.

Manifest includes:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Thanks,

Mo


Solution

  • Your problem is the way of file opening.

    For open a raw file look at below links:

    https://developer.android.com/guide/topics/resources/providing-resources

    How to read file from res/raw by name