如何在Flutter中使用原生代码解压Android和iOS的zip文件?

5
我尝试在Flutter中使用Archive 2.0.10包来解压我的zip文件。它可以运行,但与Android和iOS的本地函数相比,解压时间很长(因为我甚至已经在Android和iOS的本机应用程序中启动了它),同时还会长时间锁定主线程。如果有人帮助我实现这一点,以便我可以加快解压缩速度,我将不胜感激。在Pixel 3模拟器(1536 MB RAM)上测试需要大约5分钟或更长时间才能解压缩。如果我在Flutter上使用Compute()来独立到另一个线程,Flutter会返回“内存不足”错误。如果我在Flutter上使用Platform View来尝试在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);
  }
}

2
这里也有同样的问题。ZipDecoder decodeBytes 内存不足。 - live-love
1
我有同样的问题。我的理解是当代码执行到readAsBytesSync函数时停止工作。我还没有想出解决方案。你有吗? - Navid All Gharaee
1个回答

0
如果 archive 在您的用例中无法正常工作,我建议使用 flutter_archive 插件,因为它使用平台的本机 API 来创建和提取存档。插件中使用的本机 API 有助于内存优化。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接