@@ -318,7 +318,7 @@ private static CompressionType GetCompressionType(CFFOLDER folder)
318318 GetData ( folder ) ;
319319
320320 // Get all files for the folder
321- var files = GetFiles ( folderIndex ) ;
321+ var files = GetFiles ( filename , folderIndex ) ;
322322 if ( files . Length == 0 )
323323 return folder . DataBlocks ;
324324
@@ -337,7 +337,7 @@ private static CompressionType GetCompressionType(CFFOLDER folder)
337337 // Get all blocks from Prev
338338 if ( Prev ? . Header != null && Prev . Folders != null )
339339 {
340- int prevFolderIndex = Prev . FolderCount ;
340+ int prevFolderIndex = Prev . FolderCount - 1 ;
341341 var prevFolder = Prev . Folders [ prevFolderIndex - 1 ] ;
342342 prevBlocks = Prev . GetDataBlocks ( filename , prevFolder , prevFolderIndex , skipNext : true ) ?? [ ] ;
343343 }
@@ -387,14 +387,85 @@ public void GetData(CFFOLDER folder)
387387 }
388388 }
389389 }
390+
391+ /// <summary>
392+ /// Get all files for the current folder index
393+ /// </summary>
394+ /// <param name="folderIndex">Index of the folder in the cabinet</param>
395+ /// <param name="ignorePrev">True to ignore previous links, false otherwise</param>
396+ /// <returns>Array of all files for the folder</returns>
397+ private CFFILE [ ] GetFiles ( string ? filename , int folderIndex , bool ignorePrev = false , bool skipPrev = false , bool skipNext = false )
398+ {
399+ // Ignore invalid archives
400+ if ( Files == null )
401+ return [ ] ;
402+
403+ // Get all files with a name and matching index
404+ var files = Array . FindAll ( Files , f =>
405+ {
406+ if ( string . IsNullOrEmpty ( f . Name ) )
407+ return false ;
408+
409+ // Ignore links to previous cabinets, if required
410+ if ( ignorePrev )
411+ {
412+ if ( f . FolderIndex == FolderIndex . CONTINUED_FROM_PREV )
413+ return false ;
414+ else if ( f . FolderIndex == FolderIndex . CONTINUED_PREV_AND_NEXT )
415+ return false ;
416+ }
417+
418+ int fileFolder = GetFolderIndex ( f ) ;
419+ return fileFolder == folderIndex ;
420+ } ) ;
421+
422+ // Check if the folder spans in either direction
423+ bool spanPrev = Array . Exists ( files , f => f . FolderIndex == FolderIndex . CONTINUED_FROM_PREV || f . FolderIndex == FolderIndex . CONTINUED_PREV_AND_NEXT ) ;
424+ bool spanNext = Array . Exists ( files , f => f . FolderIndex == FolderIndex . CONTINUED_TO_NEXT || f . FolderIndex == FolderIndex . CONTINUED_PREV_AND_NEXT ) ;
425+
426+ // If the folder spans backward and Prev is not being skipped
427+ CFFILE [ ] prevFiles = [ ] ;
428+ if ( ! skipPrev && spanPrev )
429+ {
430+ // Try to get Prev if it doesn't exist
431+ if ( Prev ? . Header == null )
432+ Prev = OpenPrevious ( filename ) ;
433+
434+ // Get all files from Prev
435+ if ( Prev ? . Header != null && Prev . Folders != null )
436+ {
437+ int prevFolderIndex = Prev . FolderCount - 1 ;
438+ prevFiles = Prev . GetFiles ( filename , prevFolderIndex , skipNext : true ) ?? [ ] ;
439+ }
440+ }
441+
442+ // If the folder spans forward and Next is not being skipped
443+ CFFILE [ ] nextFiles = [ ] ;
444+ if ( ! skipNext && spanNext )
445+ {
446+ // Try to get Next if it doesn't exist
447+ if ( Next ? . Header == null )
448+ Next = OpenNext ( filename ) ;
449+
450+ // Get all files from Prev
451+ if ( Next ? . Header != null && Next . Folders != null )
452+ {
453+ var nextFolder = Next . Folders [ 0 ] ;
454+ nextFiles = Next . GetFiles ( filename , 0 , skipPrev : true ) ?? [ ] ;
455+ }
456+ }
457+
458+ // Return all found files in order
459+ return [ .. prevFiles , .. files , .. nextFiles ] ;
460+ }
390461
391462 /// <summary>
392463 /// Get all files for the current folder index
393464 /// </summary>
394465 /// <param name="folderIndex">Index of the folder in the cabinet</param>
395466 /// <param name="ignorePrev">True to ignore previous links, false otherwise</param>
396467 /// <returns>Array of all files for the folder</returns>
397- private CFFILE [ ] GetFiles ( int folderIndex , bool ignorePrev = false )
468+ private CFFILE [ ] GetFilesOld ( int folderIndex , bool ignorePrev = false )
398469 {
399470 // Ignore invalid archives
400471 if ( Files == null )
0 commit comments