gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:54 24 4
gpt4 key购买 nike

我正在开发一个项目(使用 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);

异常(exception):

"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

有人有使用 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 权限。

list 包括:

<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

关于java - Android: ZipFile() java.util.zip.ZipException: 文件太短而不能成为 zip 文件: 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52172726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com