@@ -10,7 +10,7 @@ import 'package:path/path.dart' as p;
1010import 'package:path_provider/path_provider.dart' ;
1111
1212Future <String > extractAssetOrFile (String path,
13- {bool isAsset = true , String ? targetPath}) async {
13+ {bool isAsset = true , String ? targetPath, bool checkHash = false }) async {
1414 WidgetsFlutterBinding .ensureInitialized ();
1515 PackageInfo packageInfo = await PackageInfo .fromPlatform ();
1616 final documentsOrTempDir = (defaultTargetPlatform == TargetPlatform .iOS ||
@@ -22,14 +22,33 @@ Future<String> extractAssetOrFile(String path,
2222 "${packageInfo .appName }-${packageInfo .version }-${packageInfo .buildNumber }" ,
2323 targetPath ?? p.dirname (path)));
2424
25+ String assetHash = "" ;
26+ String destHash = "" ;
27+ var hashFile = File (p.join (destDir.path, ".hash" ));
28+
2529 // re-create dir
2630 if (await destDir.exists ()) {
2731 if (kDebugMode) {
2832 // always re-create in debug mode
2933 await destDir.delete (recursive: true );
3034 } else {
31- debugPrint ("Application archive already unpacked." );
32- return destDir.path;
35+ if (checkHash) {
36+ // read asset hash from asset
37+ try {
38+ assetHash = (await rootBundle.loadString ("$path .hash" )).trim ();
39+ // ignore: empty_catches
40+ } catch (e) {}
41+ if (await hashFile.exists ()) {
42+ destHash = (await hashFile.readAsString ()).trim ();
43+ }
44+ }
45+
46+ if (assetHash != destHash) {
47+ await destDir.delete (recursive: true );
48+ } else {
49+ debugPrint ("Application archive already unpacked." );
50+ return destDir.path;
51+ }
3352 }
3453 }
3554 await destDir.create (recursive: true );
@@ -61,16 +80,24 @@ Future<String> extractAssetOrFile(String path,
6180 }
6281 }
6382
83+ if (checkHash) {
84+ await hashFile.writeAsString (assetHash);
85+ }
86+
6487 debugPrint ("Finished unpacking application archive." );
6588 return destDir.path;
6689}
6790
68- Future <String > extractAssetZip (String assetPath, {String ? targetPath}) async {
69- return extractAssetOrFile (assetPath, targetPath: targetPath);
91+ Future <String > extractAssetZip (String assetPath,
92+ {String ? targetPath, bool checkHash = false }) async {
93+ return extractAssetOrFile (assetPath,
94+ targetPath: targetPath, checkHash: checkHash);
7095}
7196
72- Future <String > extractFileZip (String filePath, {String ? targetPath}) async {
73- return extractAssetOrFile (filePath, isAsset: false , targetPath: targetPath);
97+ Future <String > extractFileZip (String filePath,
98+ {String ? targetPath, bool checkHash = false }) async {
99+ return extractAssetOrFile (filePath,
100+ isAsset: false , targetPath: targetPath, checkHash: checkHash);
74101}
75102
76103Future <String > extractAsset (String assetPath) async {
0 commit comments