@@ -16,6 +16,8 @@ public static class DecompressHelper
16
16
{
17
17
private readonly static StatusCenterViewModel _statusCenterViewModel = Ioc . Default . GetRequiredService < StatusCenterViewModel > ( ) ;
18
18
19
+ private static IThreadingService _threadingService = Ioc . Default . GetRequiredService < IThreadingService > ( ) ;
20
+
19
21
private static async Task < SevenZipExtractor ? > GetZipFile ( BaseStorageFile archive , string password = "" )
20
22
{
21
23
return await FilesystemTasks . Wrap ( async ( ) =>
@@ -40,57 +42,13 @@ public static async Task ExtractArchiveAsync(BaseStorageFile archive, BaseStorag
40
42
if ( zipFile is null )
41
43
return ;
42
44
43
- var directoryEntries = new List < ArchiveFileInfo > ( ) ;
44
- var fileEntries = new List < ArchiveFileInfo > ( ) ;
45
- foreach ( ArchiveFileInfo entry in zipFile . ArchiveFileData )
46
- {
47
- if ( ! entry . IsDirectory )
48
- fileEntries . Add ( entry ) ;
49
- else
50
- directoryEntries . Add ( entry ) ;
51
- }
52
-
53
- if ( cancellationToken . IsCancellationRequested ) // Check if cancelled
54
- return ;
55
-
56
- var directories = new List < string > ( ) ;
57
- try
58
- {
59
- directories . AddRange ( directoryEntries . Select ( ( entry ) => entry . FileName ) ) ;
60
- directories . AddRange ( fileEntries . Select ( ( entry ) => Path . GetDirectoryName ( entry . FileName ) ) ) ;
61
- }
62
- catch ( Exception ex )
63
- {
64
- App . Logger . LogWarning ( ex , $ "Error transforming zip names into: { destinationFolder . Path } \n " +
65
- $ "Directories: { string . Join ( ", " , directoryEntries . Select ( x => x . FileName ) ) } \n " +
66
- $ "Files: { string . Join ( ", " , fileEntries . Select ( x => x . FileName ) ) } ") ;
67
- return ;
68
- }
69
-
70
- foreach ( var dir in directories . Distinct ( ) . OrderBy ( x => x . Length ) )
71
- {
72
- if ( ! NativeFileOperationsHelper . CreateDirectoryFromApp ( dir , IntPtr . Zero ) )
73
- {
74
- var dirName = destinationFolder . Path ;
75
- foreach ( var component in dir . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries ) )
76
- {
77
- dirName = Path . Combine ( dirName , component ) ;
78
- NativeFileOperationsHelper . CreateDirectoryFromApp ( dirName , IntPtr . Zero ) ;
79
- }
80
- }
81
-
82
- if ( cancellationToken . IsCancellationRequested ) // Check if canceled
83
- return ;
84
- }
85
-
86
45
if ( cancellationToken . IsCancellationRequested ) // Check if canceled
87
46
return ;
88
47
89
48
// Fill files
90
49
91
50
byte [ ] buffer = new byte [ 4096 ] ;
92
- int entriesAmount = fileEntries . Count ;
93
- var minimumTime = new DateTime ( 1 ) ;
51
+ int entriesAmount = zipFile . ArchiveFileData . Where ( x => ! x . IsDirectory ) . Count ( ) ;
94
52
95
53
StatusCenterItemProgressModel fsProgress = new (
96
54
progress ,
@@ -104,47 +62,38 @@ public static async Task ExtractArchiveAsync(BaseStorageFile archive, BaseStorag
104
62
zipFile . Extracting += ( s , e ) =>
105
63
{
106
64
if ( fsProgress . TotalSize > 0 )
107
- fsProgress . Report ( ( fsProgress . ProcessedSize + e . PercentDelta / 100.0 * e . BytesCount ) / fsProgress . TotalSize * 100 ) ;
65
+ fsProgress . Report ( e . BytesProcessed / ( double ) fsProgress . TotalSize * 100 ) ;
108
66
} ;
109
-
110
- foreach ( var entry in fileEntries )
67
+ zipFile . FileExtractionStarted += ( s , e ) =>
111
68
{
112
- if ( cancellationToken . IsCancellationRequested ) // Check if canceled
113
- return ;
114
-
115
- var filePath = destinationFolder . Path ;
116
- foreach ( var component in entry . FileName . Split ( Path . DirectorySeparatorChar , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries ) )
117
- filePath = Path . Combine ( filePath , component ) ;
118
-
119
- var hFile = NativeFileOperationsHelper . CreateFileForWrite ( filePath ) ;
120
- if ( hFile . IsInvalid )
121
- return ; // TODO: handle error
122
-
123
- fsProgress . FileName = entry . FileName ;
124
- fsProgress . Report ( ) ;
125
-
126
- // We don't close hFile because FileStream.Dispose() already does that
127
- using ( FileStream destinationStream = new FileStream ( hFile , FileAccess . Write ) )
69
+ if ( cancellationToken . IsCancellationRequested )
70
+ e . Cancel = true ;
71
+ if ( ! e . FileInfo . IsDirectory )
128
72
{
129
- try
73
+ _threadingService . ExecuteOnUiThreadAsync ( ( ) =>
130
74
{
131
- await zipFile . ExtractFileAsync ( entry . Index , destinationStream ) ;
132
- }
133
- catch ( Exception ex )
134
- {
135
- App . Logger . LogWarning ( ex , $ "Error extracting file: { filePath } ") ;
136
- return ; // TODO: handle error
137
- }
75
+ fsProgress . FileName = e . FileInfo . FileName ;
76
+ fsProgress . Report ( ) ;
77
+ } ) ;
138
78
}
139
-
140
- _ = new FileInfo ( filePath )
79
+ } ;
80
+ zipFile . FileExtractionFinished += ( s , e ) =>
81
+ {
82
+ if ( ! e . FileInfo . IsDirectory )
141
83
{
142
- CreationTime = entry . CreationTime > minimumTime && entry . CreationTime < entry . LastWriteTime ? entry . CreationTime : entry . LastWriteTime ,
143
- LastWriteTime = entry . LastWriteTime ,
144
- } ;
84
+ fsProgress . AddProcessedItemsCount ( 1 ) ;
85
+ fsProgress . Report ( ) ;
86
+ }
87
+ } ;
145
88
146
- fsProgress . AddProcessedItemsCount ( 1 ) ;
147
- fsProgress . Report ( ) ;
89
+ try
90
+ {
91
+ await zipFile . ExtractArchiveAsync ( destinationFolder . Path ) ;
92
+ }
93
+ catch ( Exception ex )
94
+ {
95
+ App . Logger . LogWarning ( ex , $ "Error extracting file: { archive . Name } ") ;
96
+ return ; // TODO: handle error
148
97
}
149
98
}
150
99
0 commit comments