我正在try 从归档文件中提取文件,但遇到此错误.

E/Ffltter(31571):[ERROR:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:文件系统异常:无法打开文件,路径=‘Assets/res.Zip’(操作系统错误:没有这样的文件或目录,错误号=2)

我的密码. https://pub.dev/packages/archive/example

final bytes = File('assets/res.zip').readAsBytesSync();
final archive = ZipDecoder()
    .decodeBytes(bytes, verify: true, password: '123');

// Extract the contents of the Zip archive to disk.
for (final file in archive) {
  final filename = file.name;
  if (file.isFile) {
    final data = file.content as List<int>;
    File('out/' + filename)
      ..createSync(recursive: true)
      ..writeAsBytesSync(data);
  } else {
    Directory('out/' + filename).create(recursive: true);
  }
}

Pubspec.yaml

 assets:
    - assets/
    - assets/res.zip

推荐答案

您需要首先从Bundle 的二进制文件中获取assets资源 .对于这一点,请使用rootBundle.

rootBundle包含构建应用程序时随应用程序打包的资源.要将资源添加到应用程序的[rootBundle]中,请将它们添加到应用程序pubspec.yaml list 的flutter部分的assets小节中.

下面是一个例子:

final byteData = await rootBundle.load('assets/res.zip');
final buffer = byteData.buffer;
final archive = ZipDecoder()
    .decodeBytes(buffer.asUint8List(), verify: true, password: '123');

// ...

另一种方法是将资源解压缩到临时文件夹,如下所示:

final byteData = await rootBundle.load('assets/res.zip');
final buffer = byteData.buffer;
File file = await File('/path/to/extract/res.zip').writeAsBytes(
    buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
final bytes = File('/path/to/extract/res.zip').readAsBytesSync();
final archive =
    ZipDecoder().decodeBytes(bytes, verify: true, password: '123');

// ...

要获得/path/to/extract/路径,例如临时文件夹,请使用插件path_provider和函数getTemporaryDirectory.

设备上未备份且适合存储下载文件缓存的临时目录的路径.

Flutter相关问答推荐

如何在应用栏中将列置于中心

未处理异常:字符串类型不是值类型子类型的子类型'

就像Flutter 中的头文件一样?

Android上火焰瓷砖之间的线条飘动

'Flutter的无效常数值错误

有没有可能不在BlocBuilder中重新构建内容?

使用ImageFilter模糊并剪裁同一renderObject中的元素

不要跨同步间隔使用BuildContext

Flutter如何将2个以上的值传递到ListView上的另一个屏幕?

从图标启动器打开时,VSCode未检测到Web设备

Flutter 附近的连接

无法应用插件com.chaquo.python

使用Flutter项目设置Firebase遇到困难?这些解决方案或许能帮到你!

Flutter使Perspective Page View无限循环

如何流畅切换画面?

我正在try 使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用

使用 Chaquopy 插件后 Gradle Sync 无法正常工作

在自动完成中 Select 值后保持键盘焦点

未处理的异常:FileSystemException:无法打开文件,路径 = 'assets/res.zip'(操作系统错误:没有这样的文件或目录,errno = 2)

Flutter 评级栏包选中和未选中的容器