unzipToTemp static method

Future<String> unzipToTemp(
  1. String zipFilePath
)

Implementation

static Future<String> unzipToTemp(String zipFilePath) async {
  final sourceFile = File(zipFilePath);
  if (!await sourceFile.exists()) {
    throw Exception('Source file not found: $zipFilePath');
  }
  final tmpDir = await Directory.systemTemp.createTemp('opentool_unpack_');
  final extractDir = tmpDir.path;
  await Directory(extractDir).create(recursive: true);

  final decoded = await _decodeAndValidate(zipFilePath);
  try {
    for (final entry in decoded.archive.files) {
      final cleanName = p.posix.normalize(entry.name);
      final components = p.posix.split(cleanName);
      final filePath = p.joinAll([extractDir, ...components]);
      if (entry.isDirectory) {
        await Directory(filePath).create(recursive: true);
        continue;
      }
      await File(filePath).parent.create(recursive: true);
      final output = OutputFileStream(filePath);
      try {
        entry.writeContent(output);
        if (output.length != entry.size) {
          throw FormatException(
            'Archive entry has an invalid size: $cleanName',
          );
        }
      } finally {
        await output.close();
      }
    }
    return extractDir;
  } catch (_) {
    await Directory(tmpDir.path).delete(recursive: true);
    rethrow;
  } finally {
    await decoded.close();
  }
}