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

时间:2019-09-15 23:57:04

标签: java swift flutter unzip

我尝试使用Flutter中的Archive 2.0.10软件包解压缩zip存档。它可以工作,但是与Android和iOS的本机功能相比,解压缩的时间非常长(我之所以这样说是因为我什至是本机是为Android和iOS本地启动的),而且锁定主线程的时间也很长。如果有人能帮助我实现这一点,以便加快拆箱速度,我将不胜感激。在Pixel 3 Emulator(1536 MB Ram)上进行测试大约需要5分钟或更长时间才能解压缩。如果我在Flutter上使用Compute()隔离到另一个线程,Flutter会向我返回“ Memory Out”错误。如果我在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);
  }
}

0 个答案:

没有答案