decodeZip static method

Future<LottieComposition?> decodeZip(
  1. List<int> bytes, {
  2. LottieImageProviderFactory? imageProviderFactory,
  3. ArchiveFile? filePicker(
    1. List<ArchiveFile>
    )?,
})

Implementation

static Future<LottieComposition?> decodeZip(
  List<int> bytes, {
  LottieImageProviderFactory? imageProviderFactory,
  ArchiveFile? Function(List<ArchiveFile>)? filePicker,
}) async {
  if (bytes[0] == 0x50 && bytes[1] == 0x4B) {
    var archive = ZipDecoder().decodeBytes(bytes);

    ArchiveFile? jsonFile;
    if (filePicker != null) {
      jsonFile = filePicker(archive.files);
    }
    jsonFile ??= archive.files.firstWhere((e) => e.name.endsWith('.json'));

    var composition = parseJsonBytes(jsonFile.content as Uint8List);

    for (var image in composition.images.values) {
      var imagePath = p.posix.join(image.dirName, image.fileName);
      var found = archive.files.firstWhereOrNull(
          (f) => f.name.toLowerCase() == imagePath.toLowerCase());

      ImageProvider? provider;
      if (imageProviderFactory != null) {
        provider = imageProviderFactory(image);
      }

      if (provider != null) {
        image.loadedImage = await loadImage(composition, image, provider);
      }

      if (found != null) {
        image.loadedImage ??= await loadImage(
            composition, image, MemoryImage(found.content as Uint8List));
      }
    }

    for (var font in archive.files.where((f) => f.name.endsWith('.ttf'))) {
      var fileName = p.basenameWithoutExtension(font.name).toLowerCase();
      var existingFont = composition.fonts.values
          .firstWhereOrNull((f) => f.family.toLowerCase() == fileName);
      composition._fontsToLoad.add(FontToLoad(font.content as Uint8List,
          family: existingFont?.family));
    }
    return composition;
  }
  return null;
}