11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Linq ;
45using System . Reflection ;
56using System . Text ;
7+ using System . Text . RegularExpressions ;
68
79using Force . Blazer . Algorithms ;
810using Force . Blazer . Exe . CommandLine ;
@@ -84,6 +86,7 @@ private static int ProcessCompress(CommandLineParser<BlazerCommandLineOptions> o
8486
8587 string [ ] fileNamesMultiple = new [ ] { options . GetNonParamOptions ( 0 ) ?? string . Empty } ;
8688 string archiveName = null ;
89+ var hasUnexistingFiles = false ;
8790
8891 var listFile = options . GetNonParamOptions ( ) . FirstOrDefault ( x => x [ 0 ] == '@' ) ;
8992 if ( listFile != null )
@@ -97,9 +100,10 @@ private static int ProcessCompress(CommandLineParser<BlazerCommandLineOptions> o
97100
98101 archiveName = fileNamesMultiple [ 0 ] ;
99102 fileNamesMultiple = File . ReadAllLines ( listFile ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) . Select ( x => x . Trim ( ) ) . ToArray ( ) ;
103+ fileNamesMultiple = ExpandFilesInList ( fileNamesMultiple , out hasUnexistingFiles ) ;
100104 }
101105
102- if ( ! opt . Stdin && fileNamesMultiple . Any ( x => ! File . Exists ( x ) && ! Directory . Exists ( x ) ) )
106+ if ( ! opt . Stdin && hasUnexistingFiles )
103107 {
104108 if ( fileNamesMultiple [ 0 ] == string . Empty )
105109 {
@@ -233,7 +237,7 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
233237
234238 var archiveName = options . GetNonParamOptions ( 0 ) ?? string . Empty ;
235239
236- string [ ] customOutFileNames = null ;
240+ Regex [ ] customOutFileNames = null ;
237241
238242 var listFile = options . GetNonParamOptions ( ) . FirstOrDefault ( x => x [ 0 ] == '@' ) ;
239243 if ( listFile != null )
@@ -245,7 +249,9 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
245249 return 1 ;
246250 }
247251
248- customOutFileNames = File . ReadAllLines ( listFile ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) . Select ( x => x . Trim ( ) ) . ToArray ( ) ;
252+ customOutFileNames = File . ReadAllLines ( listFile ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) . Select ( x => x . Trim ( ) )
253+ . Select ( x => new Regex ( new string ( x . SelectMany ( y => char . IsLetterOrDigit ( y ) ? new [ ] { y } : ( y == '*' ? new [ ] { '.' , '*' } : new [ ] { '\\ ' , y } ) ) . ToArray ( ) ) ) )
254+ . ToArray ( ) ;
249255 }
250256
251257 if ( ! opt . Stdin && ! File . Exists ( archiveName ) )
@@ -272,15 +278,19 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
272278 {
273279 if ( prevFile != null )
274280 {
275- outFile [ 0 ] . Flush ( ) ;
276- outFile [ 0 ] . Close ( ) ;
277- outFile [ 0 ] = null ;
281+ if ( outFile [ 0 ] != null )
282+ {
283+ outFile [ 0 ] . Flush ( ) ;
284+ outFile [ 0 ] . Close ( ) ;
285+ outFile [ 0 ] = null ;
286+ }
287+
278288 prevFile . ApplyToFile ( ) ;
279289 prevFile = null ;
280290 }
281291
282292 var fInfoFileName = fInfo . FileName ;
283- if ( customOutFileNames != null && ! customOutFileNames . Contains ( fInfoFileName ) )
293+ if ( customOutFileNames != null && ! customOutFileNames . Any ( y => y . IsMatch ( fInfoFileName ) ) )
284294 return ;
285295
286296 prevFile = fInfo ;
@@ -343,7 +353,7 @@ private static int ProcessDecompress(CommandLineParser<BlazerCommandLineOptions>
343353
344354 if ( opt . Stdout ) outFile [ 0 ] = Console . OpenStandardOutput ( ) ;
345355 // we haven't received an file info from callback
346- if ( outFile [ 0 ] == null )
356+ if ( outFile [ 0 ] == null && ! outBlazerStream . HaveMultipleFiles )
347357 outFile [ 0 ] = new StatStream ( new FileStream ( fileName , FileMode . OpenOrCreate , FileAccess . Write , FileShare . Read ) , true ) ;
348358
349359 using ( var inFile = outStream )
@@ -536,5 +546,33 @@ private static CommandLineParser<BlazerCommandLineOptions> ParseArguments(string
536546 return null ;
537547 }
538548 }
549+
550+ private static string [ ] ExpandFilesInList ( string [ ] initialFiles , out bool hasMissingFiles )
551+ {
552+ hasMissingFiles = false ;
553+ var l = new List < string > ( ) ;
554+ // todo: better search + unit tests
555+ foreach ( var s in initialFiles )
556+ {
557+ if ( File . Exists ( s ) )
558+ l . Add ( s ) ;
559+ else if ( Directory . Exists ( s ) ) l . Add ( s ) ;
560+ else
561+ {
562+ var asteriskIdx = s . IndexOf ( "*" , StringComparison . InvariantCulture ) ;
563+ if ( asteriskIdx < 0 ) hasMissingFiles = true ;
564+ else
565+ {
566+ var slashIdx = s . LastIndexOfAny (
567+ new [ ] { Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar } , asteriskIdx - 1 , asteriskIdx - 1 ) ;
568+ var dirToSearch = string . Empty ;
569+ if ( slashIdx >= 0 ) dirToSearch = s . Substring ( 0 , slashIdx ) ;
570+ l . AddRange ( Directory . GetFiles ( dirToSearch , s . Remove ( 0 , slashIdx + 1 ) , SearchOption . AllDirectories ) ) ;
571+ }
572+ }
573+ }
574+
575+ return l . ToArray ( ) ;
576+ }
539577 }
540578}
0 commit comments