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

Android: ZipFile() java.util.zip.ZipException: File too short to be a zip file: 0… here is a solution to the problem.

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 neural networks stored as .zip files for further use of DL4J in Android.

I’m trying to open this .zip file located in my project’s res\raw directory. To do this, I tried using the ZipFile() method in java.util.zip.

Screenshot of location of neuralnet.zip

Question:

The 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”

So I can’t load the model. Test on a simulated device running Android API 24

On API 26 exceptions are different:

java.io.FileNotFoundException: File doesn’t exist: [email protected]

Does anyone have experience with ZipFile() or loading neural network models with DL4J in Android?

Do you need something special in build.gradle?

Any comments are welcome!

I tried :

  • Check the .zip file to make sure it exists.
  • Ensure that .zip

  • is just a container and that the files are not compressed by using 7Zip to archive the 3 contained files as .zip without compression
  • Checked READ_EXTERNAL_STORAGE permissions, although I don’t think it’s needed
  • Try using the full path instead of this.getResources().openRawResource(R.raw.neuralnet).

Android permissions:
I’m running android api > 23 and I’m requesting READ_EXTERNAL_STORAGE permissions at runtime.

List 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 to open the file.

To open the original file, check the following link:

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

How to read file from res/raw by name

Related Problems and Solutions