1- import 'dart:io' ;
2-
3- import 'package:archive/archive_io.dart' ;
4- import 'package:flutter/foundation.dart' ;
5- import 'package:flutter/services.dart' ;
6- import 'package:flutter/widgets.dart' ;
7- import 'package:path/path.dart' as p;
8- import 'package:path_provider/path_provider.dart' ;
1+ import 'utils_web.dart' if (dart.library.io) 'utils_io.dart' ;
92
103Future <String > extractAssetOrFile (String path,
11- {bool isAsset = true , String ? targetPath, bool checkHash = false }) async {
12- WidgetsFlutterBinding .ensureInitialized ();
13- final supportDir = await getApplicationSupportDirectory ();
14- final destDir =
15- Directory (p.join (supportDir.path, "flet" , targetPath ?? p.dirname (path)));
16-
17- String assetHash = "" ;
18- String destHash = "" ;
19- var hashFile = File (p.join (destDir.path, ".hash" ));
20-
21- // re-create dir
22- if (await destDir.exists ()) {
23- if (kDebugMode) {
24- // always re-create in debug mode
25- await destDir.delete (recursive: true );
26- } else {
27- if (checkHash) {
28- // read asset hash from asset
29- try {
30- assetHash = (await rootBundle.loadString ("$path .hash" )).trim ();
31- // ignore: empty_catches
32- } catch (e) {}
33- if (await hashFile.exists ()) {
34- destHash = (await hashFile.readAsString ()).trim ();
35- }
36- }
37-
38- if (assetHash != destHash ||
39- (checkHash && assetHash == "" && destHash == "" )) {
40- await destDir.delete (recursive: true );
41- } else {
42- debugPrint ("Application archive already unpacked to ${destDir .path }" );
43- return destDir.path;
44- }
45- }
46- }
47-
48- debugPrint ("extractAssetOrFile directory: ${destDir .path }" );
49- await destDir.create (recursive: true );
50-
51- // unpack from asset or file
52- debugPrint ("Start unpacking archive: $path " );
53- Stopwatch stopwatch = Stopwatch ()..start ();
54-
55- try {
56- Archive archive;
57- if (isAsset) {
58- final bytes = await rootBundle.load (path);
59- var data = bytes.buffer.asUint8List ();
60- archive = ZipDecoder ().decodeBytes (data);
61- } else {
62- final inputStream = InputFileStream (path);
63- archive = ZipDecoder ().decodeBuffer (inputStream);
64- }
65- await extractArchiveToDiskAsync (archive, destDir.path, asyncWrite: true );
66- } catch (e) {
67- debugPrint ("Error unpacking archive: $e " );
68- await destDir.delete (recursive: true );
69- rethrow ;
70- }
71-
72- debugPrint ("Finished unpacking application archive in ${stopwatch .elapsed }" );
73-
74- if (checkHash) {
75- await hashFile.writeAsString (assetHash);
76- }
77-
78- return destDir.path;
4+ {bool isAsset = true , String ? targetPath, bool checkHash = false }) {
5+ return getPlatformUtils ().extractAssetOrFile (path,
6+ isAsset: isAsset, targetPath: targetPath, checkHash: checkHash);
797}
808
819Future <String > extractAssetZip (String assetPath,
82- {String ? targetPath, bool checkHash = false }) async {
83- return extractAssetOrFile (assetPath,
10+ {String ? targetPath, bool checkHash = false }) {
11+ return getPlatformUtils (). extractAssetZip (assetPath,
8412 targetPath: targetPath, checkHash: checkHash);
8513}
8614
8715Future <String > extractFileZip (String filePath,
88- {String ? targetPath, bool checkHash = false }) async {
89- return extractAssetOrFile (filePath,
90- isAsset : false , targetPath: targetPath, checkHash: checkHash);
16+ {String ? targetPath, bool checkHash = false }) {
17+ return getPlatformUtils (). extractFileZip (filePath,
18+ targetPath: targetPath, checkHash: checkHash);
9119}
9220
93- Future <String > extractAsset (String assetPath) async {
94- WidgetsFlutterBinding .ensureInitialized ();
95-
96- // (re-)create destination directory
97- final supportDir = await getApplicationSupportDirectory ();
98- final destDir =
99- Directory (p.join (supportDir.path, "flet" , p.dirname (assetPath)));
100-
101- await destDir.create (recursive: true );
102-
103- // extract file from assets
104- var destPath = p.join (destDir.path, p.basename (assetPath));
105- if (kDebugMode && await File (destPath).exists ()) {
106- await File (destPath).delete ();
107- }
108- ByteData data = await rootBundle.load (assetPath);
109- List <int > bytes =
110- data.buffer.asUint8List (data.offsetInBytes, data.lengthInBytes);
111- await File (destPath).writeAsBytes (bytes);
112- return destPath;
21+ Future <String > extractAsset (String assetPath) {
22+ return getPlatformUtils ().extractAsset (assetPath);
11323}
11424
115- Future <String > getDirFiles (String path, {bool recursive = false }) async {
116- final dir = Directory (path);
117- if (! await dir.exists ()) {
118- return "<not found>" ;
119- }
120- return (await dir.list (recursive: recursive).toList ())
121- .map ((file) => file.path)
122- .join ('\n ' );
123- }
25+ Future <String > getDirFiles (String path, {bool recursive = false }) {
26+ return getPlatformUtils ().getDirFiles (path, recursive: recursive);
27+ }
0 commit comments