@@ -148,20 +148,21 @@ public static bool ExtractSet(Stream? stream, string filename, string outDir, bo
148148 try
149149 {
150150 // Loop through the cabinets
151+ bool allExtracted = true ;
151152 do
152153 {
153- current . Extract ( filename , outDir , includeDebug ) ;
154+ allExtracted &= current . Extract ( filename , outDir , includeDebug ) ;
154155 current = current . Next ?? current . OpenNext ( filename ) ;
155156 }
156157 while ( current ? . Header != null ) ;
158+
159+ return allExtracted ;
157160 }
158161 catch ( Exception ex )
159162 {
160163 if ( includeDebug ) Console . WriteLine ( ex ) ;
161164 return false ;
162165 }
163-
164- return true ;
165166 }
166167
167168 /// <summary>
@@ -180,13 +181,14 @@ public bool Extract(string? filename, string outDir, bool includeDebug)
180181 try
181182 {
182183 // Loop through the folders
184+ bool allExtracted = true ;
183185 for ( int f = 0 ; f < Folders . Length ; f ++ )
184186 {
185187 var folder = Folders [ f ] ;
186- ExtractFolder ( filename , outDir , folder , f , includeDebug ) ;
188+ allExtracted &= ExtractFolder ( filename , outDir , folder , f , includeDebug ) ;
187189 }
188190
189- return true ;
191+ return allExtracted ;
190192 }
191193 catch ( Exception ex )
192194 {
@@ -203,7 +205,8 @@ public bool Extract(string? filename, string outDir, bool includeDebug)
203205 /// <param name="folder">Folder containing the blocks to decompress</param>
204206 /// <param name="folderIndex">Index of the folder in the cabinet</param>
205207 /// <param name="includeDebug">True to include debug data, false otherwise</param>
206- private void ExtractFolder ( string ? filename ,
208+ /// <returns>True if all files extracted, false otherwise</returns>
209+ private bool ExtractFolder ( string ? filename ,
207210 string outDir ,
208211 CFFOLDER ? folder ,
209212 int folderIndex ,
@@ -212,15 +215,18 @@ private void ExtractFolder(string? filename,
212215 // Decompress the blocks, if possible
213216 using var blockStream = DecompressBlocks ( filename , folder , folderIndex ) ;
214217 if ( blockStream == null || blockStream . Length == 0 )
215- return ;
218+ return false ;
216219
217220 // Loop through the files
221+ bool allExtracted = true ;
218222 var files = GetFiles ( folderIndex ) ;
219223 for ( int i = 0 ; i < files . Length ; i ++ )
220224 {
221225 var file = files [ i ] ;
222- ExtractFile ( outDir , blockStream , file , includeDebug ) ;
226+ allExtracted &= ExtractFile ( outDir , blockStream , file , includeDebug ) ;
223227 }
228+
229+ return allExtracted ;
224230 }
225231
226232 /// <summary>
@@ -230,33 +236,39 @@ private void ExtractFolder(string? filename,
230236 /// <param name="blockStream">Stream representing the uncompressed block data</param>
231237 /// <param name="file">File information</param>
232238 /// <param name="includeDebug">True to include debug data, false otherwise</param>
233- private static void ExtractFile ( string outDir , Stream blockStream , CFFILE file , bool includeDebug )
239+ /// <returns>True if the file extracted, false otherwise</returns>
240+ private static bool ExtractFile ( string outDir , Stream blockStream , CFFILE file , bool includeDebug )
234241 {
235242 try
236243 {
237244 blockStream . Seek ( file . FolderStartOffset , SeekOrigin . Begin ) ;
238245 byte [ ] fileData = blockStream . ReadBytes ( ( int ) file . FileSize ) ;
239246
240247 // Ensure directory separators are consistent
241- string fileName = file . Name ! ;
248+ string filename = file . Name ! ;
242249 if ( Path . DirectorySeparatorChar == '\\ ' )
243- fileName = fileName . Replace ( '/' , '\\ ' ) ;
250+ filename = filename . Replace ( '/' , '\\ ' ) ;
244251 else if ( Path . DirectorySeparatorChar == '/' )
245- fileName = fileName . Replace ( '\\ ' , '/' ) ;
252+ filename = filename . Replace ( '\\ ' , '/' ) ;
246253
247- string tempFile = Path . Combine ( outDir , fileName ) ;
248- var directoryName = Path . GetDirectoryName ( tempFile ) ;
254+ // Ensure the full output directory exists
255+ filename = Path . Combine ( outDir , filename ) ;
256+ var directoryName = Path . GetDirectoryName ( filename ) ;
249257 if ( directoryName != null && ! Directory . Exists ( directoryName ) )
250258 Directory . CreateDirectory ( directoryName ) ;
251259
252- using var of = File . OpenWrite ( tempFile ) ;
253- of . Write ( fileData , 0 , fileData . Length ) ;
254- of . Flush ( ) ;
260+ // Open the output file for writing
261+ using var fs = File . OpenWrite ( filename ) ;
262+ fs . Write ( fileData , 0 , fileData . Length ) ;
263+ fs . Flush ( ) ;
255264 }
256265 catch ( Exception ex )
257266 {
258267 if ( includeDebug ) Console . WriteLine ( ex ) ;
268+ return false ;
259269 }
270+
271+ return true ;
260272 }
261273
262274 #endregion
0 commit comments