如何使用 Android 和 iOS 的本机代码在 flutter 中解压缩 zip 文件?

Osé*_*iro 5 java unzip swift flutter

我尝试使用 Flutter 中的 Archive 2.0.10 包来解压缩我的 zip 存档。可以用,但是解压的时间比Android和iOS的原生功能要长很多(我这么说是因为我什至是原生启动了Android和iOS的app),并且锁了主线程很长时间。如果有人能帮助我实现这一点,我将不胜感激,这样我就可以加快解包速度。在 Pixel 3 Emulator (1536 MB Ram) 上进行测试需要大约 5 分钟或更长时间才能解压。如果我在 Flutter 上使用 Compute() 来隔离到另一个线程,Flutter 会返回“Memory Out”错误。如果我在 Flutter 上使用平台视图,在 Android 和 iOS 上尝试使用本机功能,我是否能够简化流程?谢谢!

这是我在 Flutter 中使用的函数:

unZipFile() async {
  try{
    String path = await getDatabasesPath();
    String fileZip = ConstData.fileDBZip;

    // Le o arquivo Zip no disco
    List<int> bytes = new File("$path/$fileZip").readAsBytesSync();

    // Decodifica o arquivo Zip
    Archive archive = new ZipDecoder().decodeBytes(bytes);

    // Descompacta o arquivo Zip para o disco
    for (ArchiveFile file in archive) {
      String filename = "$path/${file.name}";
      if (file.isFile) {

        final decompressed = await compute(decompressArchiveFile, file); // file.content;
        new File(filename)
          ..createSync(recursive: true)
          ..writeAsBytesSync(decompressed);
      } else {
        await new Directory(filename).create(recursive: true);
      }
    }
  }catch(e){
    print(e);
  }
}
Run Code Online (Sandbox Code Playgroud)

Oma*_*att 0

如果archive不适用于您的用例,我建议使用flutter_archive插件,因为它使用平台的本机 API 来创建和提取档案。插件中使用的本机 API 有助于内存优化。