extractArchiveToDisk function Null safety

void extractArchiveToDisk(
  1. Archive archive,
  2. String outputPath,
  3. {bool asyncWrite = false,
  4. int? bufferSize}
)

Implementation

void extractArchiveToDisk(Archive archive, String outputPath,
    {bool asyncWrite = false, int? bufferSize}) {
  final outDir = Directory(outputPath);
  if (!outDir.existsSync()) {
    outDir.createSync(recursive: true);
  }
  for (final file in archive.files) {
    final filePath = '$outputPath${Platform.pathSeparator}${file.name}';

    if (!file.isFile || !isWithinOutputPath(outputPath, filePath)) {
      continue;
    }

    if (asyncWrite) {
      final output = File(filePath);
      output.create(recursive: true).then((f) {
        f.open(mode: FileMode.write).then((fp) {
          final bytes = file.content as List<int>;
          fp.writeFrom(bytes).then((fp) {
            file.clear();
            fp.close();
          });
        });
      });
    } else {
      final output = OutputFileStream(filePath, bufferSize: bufferSize);
      try {
        file.writeContent(output);
      } catch (err) {
        //
      }
      output.close();
    }
  }
}