unarchive method
解压
Implementation
DownloadServiceItem unarchive(DownloadServiceItem item, String downPath) {
_log('unarchive 解压 path:${item.h5Path} zip:${item.zipUrl}');
Directory saveDirct =
LocalServerConfiguration.getCurrentZipPathSyncDirectory(item.zipUrl);
final zipFile = File(downPath);
if (!zipFile.existsSync()) {
throw Exception('Local server 下载包文件路径不存在:$downPath');
}
List<int> bytes = zipFile.readAsBytesSync();
Archive archive = ZipDecoder().decodeBytes(bytes);
try {
for (final file in archive) {
final filename = file.name;
if (file.isFile) {
final data = file.content as List<int>;
String filePath = '${saveDirct.path}/$filename';
File(filePath)
..createSync(recursive: true)
..writeAsBytesSync(data);
item.filePath.add(filename);
} else {
Directory('${saveDirct.path}/$filename').create(recursive: true);
}
}
item.loadState = LoadStateType.success;
// 清理之前的缓存
File oldfile = File(downPath);
if (oldfile.existsSync()) {
oldfile.deleteSync();
}
_log('unarchive 解压成功');
return item;
} catch (e) {
_log(
'unarchive 解压失败 item: ${item.toJson().toString()} downPath: $downPath err: $e');
rethrow;
}
}