downloadAndExtract method
Downloads a .h5p file, renames it to .zip, extracts to /final.
Implementation
Future<void> downloadAndExtract(String url,
{Function(double)? onProgress}) async {
final dir = await getApplicationDocumentsDirectory();
final tempH5p = File('${dir.path}/temp.h5p');
final tempZip = File('${dir.path}/temp.zip');
final extractPath = '${dir.path}/base/final';
// Download .h5p file with progress callback
debugPrint('⬇️ Downloading H5P from $url ...');
await _dio.download(url, tempH5p.path,
onReceiveProgress: (received, total) {
if (total != -1 && onProgress != null) {
onProgress((received / total));
}
});
// Rename to .zip
if (await tempZip.exists()) await tempZip.delete();
await tempH5p.rename(tempZip.path);
// Extract
debugPrint('📦 Extracting H5P ...');
final bytes = await tempZip.readAsBytes();
final archive = ZipDecoder().decodeBytes(bytes);
for (final file in archive) {
final filename = '$extractPath/${file.name}';
if (file.isFile) {
final outFile = File(filename)..createSync(recursive: true);
await outFile.writeAsBytes(file.content as List<int>);
} else {
Directory(filename).createSync(recursive: true);
}
}
debugPrint('✅ Extraction done → $extractPath');
}