@@ -18,32 +18,15 @@ public static class DecompressHelper
18
18
19
19
private static IThreadingService _threadingService = Ioc . Default . GetRequiredService < IThreadingService > ( ) ;
20
20
21
- private static async Task < SevenZipExtractor ? > GetZipFile ( BaseStorageFile archive , string password = "" )
22
- {
23
- return await FilesystemTasks . Wrap ( async ( ) =>
24
- {
25
- var arch = new SevenZipExtractor ( await archive . OpenStreamForReadAsync ( ) , password ) ;
26
- return arch ? . ArchiveFileData is null ? null : arch ; // Force load archive (1665013614u)
27
- } ) ;
28
- }
29
-
30
- public static async Task < bool > IsArchiveEncrypted ( BaseStorageFile archive )
31
- {
32
- using SevenZipExtractor ? zipFile = await GetZipFile ( archive ) ;
33
- if ( zipFile is null )
34
- return true ;
35
-
36
- return zipFile . ArchiveFileData . Any ( file => file . Encrypted || file . Method . Contains ( "Crypto" ) || file . Method . Contains ( "AES" ) ) ;
37
- }
38
-
39
- public static async Task ExtractArchiveAsync ( BaseStorageFile archive , BaseStorageFolder destinationFolder , string password , IProgress < StatusCenterItemProgressModel > progress , CancellationToken cancellationToken )
21
+ public static async Task < bool > DecompressArchiveAsync ( BaseStorageFile archive , BaseStorageFolder destinationFolder , string password , IProgress < StatusCenterItemProgressModel > progress , CancellationToken cancellationToken )
40
22
{
41
23
using SevenZipExtractor ? zipFile = await GetZipFile ( archive , password ) ;
42
24
if ( zipFile is null )
43
- return ;
25
+ return false ;
44
26
45
- if ( cancellationToken . IsCancellationRequested ) // Check if canceled
46
- return ;
27
+ // Check if the decompress operation canceled
28
+ if ( cancellationToken . IsCancellationRequested )
29
+ return false ;
47
30
48
31
// Fill files
49
32
@@ -88,34 +71,55 @@ public static async Task ExtractArchiveAsync(BaseStorageFile archive, BaseStorag
88
71
89
72
try
90
73
{
74
+ // TODO: Get this method return result
91
75
await zipFile . ExtractArchiveAsync ( destinationFolder . Path ) ;
92
76
}
93
77
catch ( Exception ex )
94
78
{
95
79
App . Logger . LogWarning ( ex , $ "Error extracting file: { archive . Name } ") ;
96
- return ; // TODO: handle error
80
+
81
+ return false ;
97
82
}
83
+
84
+ return true ;
98
85
}
99
86
100
- private static async Task ExtractArchiveAsync ( BaseStorageFile archive , BaseStorageFolder ? destinationFolder , string password )
87
+ private static async Task DecompressArchiveAsync ( BaseStorageFile archive , BaseStorageFolder ? destinationFolder , string password )
101
88
{
102
89
if ( archive is null || destinationFolder is null )
103
90
return ;
104
91
105
- var banner = StatusCenterHelper . AddCard_Decompress (
92
+ // Initialize a new in-progress status card
93
+ var statusCard = StatusCenterHelper . AddCard_Decompress (
106
94
archive . Path . CreateEnumerable ( ) ,
107
95
destinationFolder . Path . CreateEnumerable ( ) ,
108
96
ReturnResult . InProgress ) ;
109
97
110
- await FilesystemTasks . Wrap ( ( ) =>
111
- ExtractArchiveAsync ( archive , destinationFolder , password , banner . ProgressEventSource , banner . CancellationToken ) ) ;
98
+ // Operate decompress
99
+ var result = await FilesystemTasks . Wrap ( ( ) =>
100
+ DecompressArchiveAsync ( archive , destinationFolder , password , statusCard . ProgressEventSource , statusCard . CancellationToken ) ) ;
112
101
113
- _statusCenterViewModel . RemoveItem ( banner ) ;
102
+ // Remove the in-progress status card
103
+ _statusCenterViewModel . RemoveItem ( statusCard ) ;
114
104
115
- StatusCenterHelper . AddCard_Decompress (
116
- archive . Path . CreateEnumerable ( ) ,
117
- destinationFolder . Path . CreateEnumerable ( ) ,
118
- ReturnResult . Success ) ;
105
+ if ( result . Result )
106
+ {
107
+ // Successful
108
+ StatusCenterHelper . AddCard_Decompress (
109
+ archive . Path . CreateEnumerable ( ) ,
110
+ destinationFolder . Path . CreateEnumerable ( ) ,
111
+ ReturnResult . Success ) ;
112
+ }
113
+ else
114
+ {
115
+ // Error
116
+ StatusCenterHelper . AddCard_Decompress (
117
+ archive . Path . CreateEnumerable ( ) ,
118
+ destinationFolder . Path . CreateEnumerable ( ) ,
119
+ statusCard . CancellationToken . IsCancellationRequested
120
+ ? ReturnResult . Cancelled
121
+ : ReturnResult . Failed ) ;
122
+ }
119
123
}
120
124
121
125
public static async Task DecompressArchiveAsync ( IShellPage associatedInstance )
@@ -161,7 +165,7 @@ public static async Task DecompressArchiveAsync(IShellPage associatedInstance)
161
165
destinationFolder = await FilesystemTasks . Wrap ( ( ) => parentFolder . CreateFolderAsync ( Path . GetFileName ( destinationFolderPath ) , CreationCollisionOption . GenerateUniqueName ) . AsTask ( ) ) ;
162
166
}
163
167
164
- await ExtractArchiveAsync ( archive , destinationFolder , password ) ;
168
+ await DecompressArchiveAsync ( archive , destinationFolder , password ) ;
165
169
166
170
if ( decompressArchiveViewModel . OpenDestinationFolderOnCompletion )
167
171
await NavigationHelpers . OpenPath ( destinationFolderPath , associatedInstance , FilesystemItemType . Directory ) ;
@@ -199,7 +203,7 @@ public static async Task DecompressArchiveHereAsync(IShellPage associatedInstanc
199
203
password = Encoding . UTF8 . GetString ( decompressArchiveViewModel . Password ) ;
200
204
}
201
205
202
- await ExtractArchiveAsync ( archive , currentFolder , password ) ;
206
+ await DecompressArchiveAsync ( archive , currentFolder , password ) ;
203
207
}
204
208
}
205
209
@@ -239,8 +243,26 @@ public static async Task DecompressArchiveToChildFolderAsync(IShellPage associat
239
243
if ( currentFolder is not null )
240
244
destinationFolder = await FilesystemTasks . Wrap ( ( ) => currentFolder . CreateFolderAsync ( Path . GetFileNameWithoutExtension ( archive . Path ) , CreationCollisionOption . GenerateUniqueName ) . AsTask ( ) ) ;
241
245
242
- await ExtractArchiveAsync ( archive , destinationFolder , password ) ;
246
+ await DecompressArchiveAsync ( archive , destinationFolder , password ) ;
243
247
}
244
248
}
249
+
250
+ private static async Task < SevenZipExtractor ? > GetZipFile ( BaseStorageFile archive , string password = "" )
251
+ {
252
+ return await FilesystemTasks . Wrap ( async ( ) =>
253
+ {
254
+ var arch = new SevenZipExtractor ( await archive . OpenStreamForReadAsync ( ) , password ) ;
255
+ return arch ? . ArchiveFileData is null ? null : arch ; // Force load archive (1665013614u)
256
+ } ) ;
257
+ }
258
+
259
+ private static async Task < bool > IsArchiveEncrypted ( BaseStorageFile archive )
260
+ {
261
+ using SevenZipExtractor ? zipFile = await GetZipFile ( archive ) ;
262
+ if ( zipFile is null )
263
+ return true ;
264
+
265
+ return zipFile . ArchiveFileData . Any ( file => file . Encrypted || file . Method . Contains ( "Crypto" ) || file . Method . Contains ( "AES" ) ) ;
266
+ }
245
267
}
246
268
}
0 commit comments