11using Connect . LanguagePackManager . Core . Common ;
22using System . IO ;
33using System . IO . Compression ;
4+ using System . Runtime . CompilerServices ;
45using System . Text . RegularExpressions ;
56
67namespace Connect . LanguagePackManager . Core . Helpers
78{
8- public class ZipHelper
9+ public class ZipHelper
10+ {
11+ public static UnzipResult Unzip ( Stream fileStream , string packageBasePath , bool allowTranslations )
912 {
10- public static UnzipResult Unzip ( Stream fileStream , string packageBasePath , bool allowTranslations )
13+ var result = new UnzipResult ( packageBasePath ) ;
14+ using ( var objZipInputStream = new ZipArchive ( fileStream , ZipArchiveMode . Read ) )
15+ {
16+ foreach ( var entry in objZipInputStream . Entries )
1117 {
12- var result = new UnzipResult ( packageBasePath ) ;
13- using ( var objZipInputStream = new ZipArchive ( fileStream , ZipArchiveMode . Read ) )
14- {
15- foreach ( var entry in objZipInputStream . Entries )
18+ var fullName = entry . FullName . Trim ( '/' ) . ToLowerInvariant ( ) ;
19+ var ext = Path . GetExtension ( fullName ) ;
20+ switch ( ext )
21+ {
22+ case ".dnn" :
23+ entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , entry . Name ) , true ) ;
24+ result . ManifestFile = entry . Name ;
25+ result . ManifestFilePath = Path . GetDirectoryName ( entry . FullName ) ;
26+ break ;
27+ case ".dll" :
28+ if ( entry . Name . ToLowerInvariant ( ) == "dotnetnuke.dll" )
29+ {
30+ entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , entry . Name ) , true ) ;
31+ result . DnnVersion = Globals . GetAssemblyVersion ( Path . Combine ( result . UnzipDirectory , entry . Name ) ) ;
32+ try
1633 {
17- var fullName = entry . FullName . Trim ( '/' ) . ToLowerInvariant ( ) ;
18- var ext = Path . GetExtension ( fullName ) ;
19- switch ( ext )
20- {
21- case ".dnn" :
22- entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , entry . Name ) , true ) ;
23- result . ManifestFile = entry . Name ;
24- break ;
25- case ".dll" :
26- if ( entry . Name . ToLowerInvariant ( ) == "dotnetnuke.dll" )
27- {
28- entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , entry . Name ) , true ) ;
29- result . DnnVersion = Globals . GetAssemblyVersion ( Path . Combine ( result . UnzipDirectory , entry . Name ) ) ;
30- try
31- {
32- File . Delete ( Path . Combine ( result . UnzipDirectory , entry . Name ) ) ;
33- }
34- catch
35- {
36- }
37- }
38- break ;
39- case ".resx" :
40- var fileName = fullName . ToMD5Hash ( ) ;
41- var m = Regex . Match ( entry . Name , @"\.(\w{2,3}-\w\w)\." ) ;
42- if ( allowTranslations || ! m . Success || m . Groups [ 1 ] . Value . ToLower ( ) == "en-us" ) // filter out all files that are not default locale
43- {
44- entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , fileName ) ) ;
45- result . AddResourceFile ( fullName , fileName ) ;
46- }
47- break ;
48- case ".resources" :
49- case ".zip" :
50- var zipName = fullName . ToMD5Hash ( ) ;
51- entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , zipName ) ) ;
52- result . AddZipFile ( fullName , zipName ) ;
53- break ;
54- }
34+ File . Delete ( Path . Combine ( result . UnzipDirectory , entry . Name ) ) ;
5535 }
56- }
57- return result ;
36+ catch
37+ {
38+ }
39+ }
40+ break ;
41+ case ".resx" :
42+ var fileName = fullName . ToMD5Hash ( ) ;
43+ var m = Regex . Match ( entry . Name , @"\.(\w{2,3}-\w\w)\." ) ;
44+ if ( allowTranslations || ! m . Success || m . Groups [ 1 ] . Value . ToLower ( ) == "en-us" ) // filter out all files that are not default locale
45+ {
46+ entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , fileName ) ) ;
47+ result . AddResourceFile ( fullName , fileName ) ;
48+ }
49+ break ;
50+ case ".resources" :
51+ case ".zip" :
52+ var zipName = fullName . ToMD5Hash ( ) ;
53+ entry . ExtractToFile ( Path . Combine ( result . UnzipDirectory , zipName ) ) ;
54+ result . AddZipFile ( fullName , zipName ) ;
55+ break ;
56+ }
5857 }
59-
60- public static UnzipResult Unzip ( string filePath , string packageBasePath , bool allowTranslations )
58+ }
59+ if ( ! string . IsNullOrEmpty ( result . ManifestFilePath ) )
60+ {
61+ var l = result . ManifestFilePath . Length + 1 ;
62+ foreach ( var resx in result . ResourceFiles . Values )
6163 {
62- using ( var fileStrm = File . Open ( filePath , FileMode . Open , FileAccess . Read ) )
63- {
64- return Unzip ( fileStrm , packageBasePath , allowTranslations ) ;
65- }
64+ resx . FilePath = resx . FilePath . Remove ( 0 , l ) ;
65+ resx . FilePathLowered = resx . FilePathLowered . Remove ( 0 , l ) ;
6666 }
67+ }
68+ return result ;
69+ }
70+
71+ public static UnzipResult Unzip ( string filePath , string packageBasePath , bool allowTranslations )
72+ {
73+ using ( var fileStrm = File . Open ( filePath , FileMode . Open , FileAccess . Read ) )
74+ {
75+ return Unzip ( fileStrm , packageBasePath , allowTranslations ) ;
76+ }
6777 }
78+ }
6879}
0 commit comments