@@ -21,30 +21,27 @@ public class UnsupportedWIMException : Exception { }
2121
2222 public class UnsupportedWIMXmlException : Exception { }
2323
24- private static string ExtractWIMXml ( Stream wimstream )
24+ private static string ExtractWIMXml ( ArchiveFile archiveFile )
2525 {
2626 try
2727 {
28- using ( ArchiveFile archiveFile = new ArchiveFile ( wimstream , SevenZipFormat . Wim ) )
28+ if ( archiveFile . Entries . Any ( x => x . FileName == "[1].xml" ) )
2929 {
30- if ( archiveFile . Entries . Any ( x => x . FileName == "[1].xml" ) )
31- {
32- Entry wimXmlEntry = archiveFile . Entries . First ( x => x . FileName == "[1].xml" ) ;
33-
34- string xml ;
35- using ( MemoryStream memoryStream = new MemoryStream ( ) )
36- {
37- wimXmlEntry . Extract ( memoryStream ) ;
30+ Entry wimXmlEntry = archiveFile . Entries . First ( x => x . FileName == "[1].xml" ) ;
3831
39- xml = Encoding . Unicode . GetString ( memoryStream . ToArray ( ) , 2 , ( int ) memoryStream . Length - 2 ) ;
40- }
41-
42- return xml ;
43- }
44- else if ( archiveFile . Entries . Any ( x => x . FileName == "Windows" ) )
32+ string xml ;
33+ using ( MemoryStream memoryStream = new MemoryStream ( ) )
4534 {
46- return archiveFile . GetArchiveComment ( ) ;
35+ wimXmlEntry . Extract ( memoryStream ) ;
36+
37+ xml = Encoding . Unicode . GetString ( memoryStream . ToArray ( ) , 2 , ( int ) memoryStream . Length - 2 ) ;
4738 }
39+
40+ return xml ;
41+ }
42+ else if ( archiveFile . Entries . Any ( x => x . FileName == "Windows" ) )
43+ {
44+ return archiveFile . GetArchiveComment ( ) ;
4845 }
4946
5047 throw new UnsupportedWIMException ( ) ;
@@ -80,22 +77,26 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
8077
8178 Console . WriteLine ( "Gathering WIM information XML file" ) ;
8279
83- string xml = ExtractWIMXml ( wimstream ) ;
80+
81+ using ArchiveFile archiveFile = new ArchiveFile ( wimstream , SevenZipFormat . Wim ) ;
82+ string xml = ExtractWIMXml ( archiveFile ) ;
8483
8584 Console . WriteLine ( "Parsing WIM information XML file" ) ;
8685 XmlFormats . WIMXml . WIM wim = GetWIMClassFromXml ( xml ) ;
8786
8887 Console . WriteLine ( $ "Found { wim . IMAGE . Length } images in the wim according to the XML") ;
8988
9089 Console . WriteLine ( "Evaluating relevant images in the WIM according to the XML" ) ;
91- int irelevantcount2 = ( wim . IMAGE . Any ( x => x . DESCRIPTION . Contains ( "winpe" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
92- ( wim . IMAGE . Any ( x => x . DESCRIPTION . Contains ( "setup" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
93- ( wim . IMAGE . Any ( x => x . DESCRIPTION . Contains ( "preinstallation" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
94- ( wim . IMAGE . Any ( x => x . DESCRIPTION . Contains ( "winre" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
95- ( wim . IMAGE . Any ( x => x . DESCRIPTION . Contains ( "recovery" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) ;
90+ int irelevantcount2 = ( wim . IMAGE . Any ( x => x . DESCRIPTION != null && x . DESCRIPTION . Contains ( "winpe" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
91+ ( wim . IMAGE . Any ( x => x . DESCRIPTION != null && x . DESCRIPTION . Contains ( "setup" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
92+ ( wim . IMAGE . Any ( x => x . DESCRIPTION != null && x . DESCRIPTION . Contains ( "preinstallation" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
93+ ( wim . IMAGE . Any ( x => x . DESCRIPTION != null && x . DESCRIPTION . Contains ( "winre" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) +
94+ ( wim . IMAGE . Any ( x => x . DESCRIPTION != null && x . DESCRIPTION . Contains ( "recovery" , StringComparison . InvariantCultureIgnoreCase ) ) ? 1 : 0 ) ;
9695
9796 Console . WriteLine ( $ "Found { irelevantcount2 } irrelevant images in the wim according to the XML") ;
9897
98+ var provider = new WIMInstallProviderInterface ( archiveFile ) ;
99+
99100 foreach ( var image in wim . IMAGE )
100101 {
101102 Console . WriteLine ( ) ;
@@ -105,11 +106,11 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
105106 // If what we're trying to identify isn't just a winpe, and we are accessing a winpe image
106107 // skip the image
107108 //
108- int irelevantcount = ( image . DESCRIPTION . Contains ( "winpe" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
109- ( image . DESCRIPTION . Contains ( "setup" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
110- ( image . DESCRIPTION . Contains ( "preinstallation" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
111- ( image . DESCRIPTION . Contains ( "winre" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
112- ( image . DESCRIPTION . Contains ( "recovery" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) ;
109+ int irelevantcount = ( image . DESCRIPTION != null && image . DESCRIPTION . Contains ( "winpe" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
110+ ( image . DESCRIPTION != null && image . DESCRIPTION . Contains ( "setup" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
111+ ( image . DESCRIPTION != null && image . DESCRIPTION . Contains ( "preinstallation" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
112+ ( image . DESCRIPTION != null && image . DESCRIPTION . Contains ( "winre" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) +
113+ ( image . DESCRIPTION != null && image . DESCRIPTION . Contains ( "recovery" , StringComparison . InvariantCultureIgnoreCase ) ? 1 : 0 ) ;
113114
114115 Console . WriteLine ( $ "Index contains { irelevantcount } flags indicating this is a preinstallation environment") ;
115116
@@ -125,7 +126,6 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
125126
126127 if ( index != null && wim . IMAGE [ 0 ] . INDEX == "0" )
127128 {
128- using ArchiveFile archiveFile = new ArchiveFile ( wimstream , SevenZipFormat . Wim ) ;
129129 if ( ! archiveFile . Entries . Any ( x => x . FileName . StartsWith ( "0\\ " ) ) )
130130 {
131131 WorkaroundForWIMFormatBug = true ;
@@ -140,12 +140,10 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
140140
141141 Console . WriteLine ( $ "Index value: { index } ") ;
142142
143- var provider = new WIMInstallProviderInterface ( wimstream , index ) ;
143+ provider . SetIndex ( index ) ;
144144
145145 var report = InstalledImage . DetectionHandler . IdentifyWindowsNT ( provider ) ;
146146
147- provider . Close ( ) ;
148-
149147 // fallback
150148 if ( ( string . IsNullOrEmpty ( report . Sku ) || report . Sku == "TerminalServer" ) && ! string . IsNullOrEmpty ( image . FLAGS ) )
151149 {
@@ -205,7 +203,7 @@ private static WindowsImageIndex[] IdentifyWindowsNTFromWIM(Stream wimstream, bo
205203 results . Add ( imageIndex ) ;
206204 }
207205
208- wimstream . Dispose ( ) ;
206+ provider . Close ( ) ;
209207
210208 return results . ToArray ( ) ;
211209 }
@@ -575,7 +573,7 @@ public static FileItem[] IdentifyWindowsFromISO(string isopath, bool deep, bool
575573 using FileStream isoStream = File . Open ( isopath , FileMode . Open , FileAccess . Read ) ;
576574
577575 VfsFileSystemFacade cd = new CDReader ( isoStream , true ) ;
578- if ( cd . FileExists ( @"README.TXT" ) )
576+ if ( cd . FileExists ( @"README.TXT" ) || cd . Root . GetDirectories ( ) . Length == 0 )
579577 {
580578 cd = new UdfReader ( isoStream ) ;
581579 }
@@ -646,6 +644,7 @@ public static WindowsImageIndex[] IdentifyWindowsFromWIM(Stream wim, bool includ
646644 Console . WriteLine ( ex . ToString ( ) ) ;
647645 }
648646
647+ wim . Dispose ( ) ;
649648 return result ;
650649 }
651650
0 commit comments