@@ -7,24 +7,27 @@ namespace GitHub.Unity
7
7
class UnzipTask : TaskBase
8
8
{
9
9
private readonly string archiveFilePath ;
10
- private readonly string extractedPath ;
10
+ private readonly NPath extractedPath ;
11
11
private readonly IZipHelper zipHelper ;
12
+ private readonly IFileSystem fileSystem ;
13
+ private readonly string expectedMD5 ;
12
14
private readonly IProgress < float > zipFileProgress ;
13
15
private readonly IProgress < long > estimatedDurationProgress ;
14
16
15
- public UnzipTask ( CancellationToken token , string archiveFilePath , string extractedPath ,
16
- IProgress < float > zipFileProgress = null , IProgress < long > estimatedDurationProgress = null ) :
17
- this ( token , archiveFilePath , extractedPath , ZipHelper . Instance , zipFileProgress , estimatedDurationProgress )
17
+ public UnzipTask ( CancellationToken token , string archiveFilePath , NPath extractedPath , IFileSystem fileSystem , string expectedMD5 = null , IProgress < float > zipFileProgress = null , IProgress < long > estimatedDurationProgress = null ) :
18
+ this ( token , archiveFilePath , extractedPath , ZipHelper . Instance , fileSystem , expectedMD5 , zipFileProgress , estimatedDurationProgress )
18
19
{
19
20
20
21
}
21
22
22
- public UnzipTask ( CancellationToken token , string archiveFilePath , string extractedPath , IZipHelper zipHelper , IProgress < float > zipFileProgress = null , IProgress < long > estimatedDurationProgress = null )
23
+ public UnzipTask ( CancellationToken token , string archiveFilePath , NPath extractedPath , IZipHelper zipHelper , IFileSystem fileSystem , string expectedMD5 = null , IProgress < float > zipFileProgress = null , IProgress < long > estimatedDurationProgress = null )
23
24
: base ( token )
24
25
{
25
26
this . archiveFilePath = archiveFilePath ;
26
27
this . extractedPath = extractedPath ;
27
28
this . zipHelper = zipHelper ;
29
+ this . fileSystem = fileSystem ;
30
+ this . expectedMD5 = expectedMD5 ;
28
31
this . zipFileProgress = zipFileProgress ;
29
32
this . estimatedDurationProgress = estimatedDurationProgress ;
30
33
}
@@ -33,16 +36,43 @@ protected override void Run(bool success)
33
36
{
34
37
base . Run ( success ) ;
35
38
36
- UnzipArchive ( ) ;
37
- }
38
-
39
- private void UnzipArchive ( )
40
- {
41
39
Logger . Trace ( "Unzip File: {0} to Path: {1}" , archiveFilePath , extractedPath ) ;
42
40
43
- zipHelper . Extract ( archiveFilePath , extractedPath , Token , zipFileProgress , estimatedDurationProgress ) ;
41
+ try
42
+ {
43
+ zipHelper . Extract ( archiveFilePath , extractedPath , Token , zipFileProgress , estimatedDurationProgress ) ;
44
+ }
45
+ catch ( Exception ex )
46
+ {
47
+ var message = "Error Unzipping file" ;
48
+
49
+ Logger . Error ( ex , message ) ;
50
+ throw new UnzipTaskException ( message ) ;
51
+ }
52
+
53
+ if ( expectedMD5 != null )
54
+ {
55
+ var calculatedMD5 = fileSystem . CalculateFolderMD5 ( extractedPath ) ;
56
+ if ( ! calculatedMD5 . Equals ( expectedMD5 , StringComparison . InvariantCultureIgnoreCase ) )
57
+ {
58
+ extractedPath . DeleteIfExists ( ) ;
59
+
60
+ var message = $ "Extracted MD5: { calculatedMD5 } Does not match expected: { expectedMD5 } ";
61
+ Logger . Error ( message ) ;
62
+
63
+ throw new UnzipTaskException ( message ) ;
64
+ }
65
+ }
44
66
45
67
Logger . Trace ( "Completed Unzip" ) ;
46
68
}
47
69
}
70
+
71
+ public class UnzipTaskException : Exception {
72
+ public UnzipTaskException ( string message ) : base ( message )
73
+ { }
74
+
75
+ public UnzipTaskException ( string message , Exception innerException ) : base ( message , innerException )
76
+ { }
77
+ }
48
78
}
0 commit comments