Android: ZipFile() java.util.zip.ZipException: 文件太短不能成为 zip 文件: 0

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

我正在开发一个项目(使用 Android Studio 3.1.4),该项目读取存储为 .zip 文件的神经网络,以便在 Android 中进一步与 DL4J 一起使用。

我正在尝试打开位于我的项目 res\raw 目录中的这个 .zip 文件。为此,我尝试使用 java.util.zip.

中的 ZipFile() 方法

Screenshot of location of neuralnet.zip

问题:

以下代码抛出异常:

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

异常:

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

因此我无法加载模型。在模拟设备上测试 运行 Android API 24

关于API 26 异常是不同的:

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

有人在 Android 中使用过 ZipFile() 或使用 DL4J 加载神经网络模型吗?

在 build.gradle 中是否需要一些特殊的东西?

欢迎任何意见!

我试过的:

Android 权限: 我是 运行 android api > 23,我在运行时请求 READ_EXTERNAL_STORAGE 权限。

清单包括:

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

谢谢,

你的问题是文件的打开方式。

要打开原始文件,请查看以下链接:

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

How to read file from res/raw by name

我能够打开存档的内容,方法是解决 ZipFile() 方法,该方法给我另一个错误,因为它需要路径,而不是资源标识符。

还是打开归档文件,我用的是"No Body"提出的方法:

InputStream ins1 = getResources().openRawResource(getResources().getIdentifier("configuration", "raw", getPackageName()));
InputStream ins2 = getResources().openRawResource(getResources().getIdentifier("coefficients", "raw", getPackageName()));
InputStream ins3 = getResources().openRawResource(getResources().getIdentifier("updaterstate", "raw", getPackageName()));
model = restoreMultiLayerNetwork(ins1, ins2, ins3, false);