简体   繁体   中英

How to unzip a zip file in flutter using native code for Android and iOS?

I tried using the Archive 2.0.10 package in Flutter to unzip my zip archive. It works, but the time to unzip is very long compared to the native function of Android and iOS (I say that because I even started the app natively for Android and iOS), as well as locking the Main Thread for a long time. I would appreciate if anyone would help me implement this so that I can expedite the unpacking. Testing on the Pixel 3 Emulator (1536 MB Ram) takes around 5 minutes or more to unzip. If I use Compute () on Flutter to Isolate to another Thread, Flutter returns me a "Memory Out" error. If I use Platform View on Flutter, to try this with native function on Android and iOS, would I be able to streamline the process? Thanks!

This is the function I used in 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);
  }
}

If archive doesn't work well for your use case, I suggest using flutter_archive plugin since this uses native APIs of the platforms for creating and extracting archives. Native APIs used in the plugin helps with memory optimization.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM