【问题标题】:Android: ZipFile() java.util.zip.ZipException: File too short to be a zip file: 0Android:ZipFile() java.util.zip.ZipException:文件太短而不能成为 zip 文件:0
【发布时间】:2018-09-04 19:19:42
【问题描述】:

我正在开发一个项目(使用 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: 文件太短,不能成为 zip 文件:0"

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

在 API 26 上,例外情况有所不同:

java.io.FileNotFoundException:文件不存在:android.content.res.AssetManager$AssetInputStream@ddde727

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

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

感谢任何输入!

我尝试过的:

  • 检查 .zip 文件确保它存在。
  • 确保 .zip 只是一个容器,并且文件没有被压缩,方法是使用 7Zip 将包含的 3 个文件归档为 .zip,而无需压缩
  • 检查了 READ_EXTERNAL_STORAGE 权限,尽管我认为它不应该是必需的
  • 尝试使用完整路径而不是使用 this.getResources().openRawResource(R.raw.neuralnet)

安卓权限: 我正在运行 android api > 23,并且在运行时请求 READ_EXTERNAL_STORAGE 权限。

清单包括:

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

谢谢,

【问题讨论】:

    标签: java android keras zip dl4j


    【解决方案1】:

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

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

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

    How to read file from res/raw by name

    【讨论】:

      【解决方案2】:

      我能够通过解决给我另一个错误的 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);
      

      【讨论】:

        猜你喜欢
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多