【问题标题】:How to unzip a zip file in flutter using native code for Android and iOS?如何使用 Android 和 iOS 的本机代码解压缩 zip 文件?
【发布时间】:2020-01-16 19:53:45
【问题描述】:

我尝试使用 Flutter 中的 Archive 2.0.10 包来解压缩我的 zip 存档。可以,但是解压的时间相比安卓和iOS的原生功能来说是很长的(我这么说是因为我什至在安卓和iOS上都原生启动了这个应用),以及长时间锁定主线程。如果有人能帮助我实现这一点,我将不胜感激,以便我可以加快拆包速度。在 Pixel 3 Emulator (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);
  }
}

【问题讨论】:

  • 这里有同样的问题。 ZipDecoder decodeBytes 内存不足
  • 我也有同样的问题。我所理解的是代码在到达readAsBytesSync 函数时停止工作。我还没有想出解决办法。你有吗?

标签: java swift flutter unzip


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2023-03-21
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多