11using System ;
22using System . IO ;
3+ using System . Linq ;
34using System . Reflection ;
45
56using Force . Blazer . Algorithms ;
@@ -16,95 +17,127 @@ static Program()
1617#endif
1718 }
1819
19- public static void Main ( string [ ] args )
20+ private static string GetBlazerLibraryVersion ( )
2021 {
22+ return "library: "
23+ + ( ( AssemblyFileVersionAttribute )
24+ typeof ( BlazerInputStream ) . Assembly . GetCustomAttributes ( typeof ( AssemblyFileVersionAttribute ) , true ) . First ( ) ) . Version ;
25+ }
26+
27+ public static int Main ( string [ ] args )
28+ {
29+ File . AppendAllText ( @"F:\work\Blazer\source\Blazer.Exe\bin\Release\cmd.txt" , Environment . CommandLine + Environment . NewLine ) ;
2130 var options = ParseArguments ( args ) ;
2231 if ( options == null )
23- return ;
32+ return 0 ;
2433 if ( /*options.GetNonParamOptions().Length == 0 ||*/ options . Get ( ) == null || options . Get ( ) . Help )
2534 {
26- Console . WriteLine ( options . GenerateHeader ( ) ) ;
35+ Console . WriteLine ( options . GenerateHeader ( GetBlazerLibraryVersion ( ) ) ) ;
2736 Console . WriteLine ( ) ;
2837 Console . WriteLine ( options . GenerateHelp ( ) ) ;
2938 // Console.Error.WriteLine("Please, specify input file name");
30- return ;
39+ return 0 ;
3140 }
3241
3342 try
3443 {
35- Process ( options ) ;
44+ return Process ( options ) ;
3645 }
3746 catch ( Exception ex )
3847 {
3948 Console . Error . WriteLine ( ex . Message ) ;
49+ return 1 ;
4050 }
4151 }
4252
43- private static void Process ( CommandLineParser < BlazerCommandLineOptions > options )
53+ private static int Process ( CommandLineParser < BlazerCommandLineOptions > options )
4454 {
4555 var opt = options . Get ( ) ;
4656
4757 if ( ! opt . Stdout )
4858 {
49- Console . WriteLine ( options . GenerateHeader ( ) ) ;
59+ Console . WriteLine ( options . GenerateHeader ( GetBlazerLibraryVersion ( ) ) ) ;
5060 Console . WriteLine ( ) ;
5161 }
5262
5363 if ( opt . Decompress )
5464 {
55- ProcessDecompress ( options ) ;
65+ return ProcessDecompress ( options ) ;
5666 }
5767 else if ( opt . Test )
5868 {
59- ProcessTest ( options ) ;
69+ return ProcessTest ( options ) ;
70+ }
71+ else if ( opt . List )
72+ {
73+ return ProcessList ( options ) ;
6074 }
6175 else
6276 {
63- ProcessCompress ( options ) ;
77+ return ProcessCompress ( options ) ;
6478 }
6579 }
6680
67- private static void ProcessCompress ( CommandLineParser < BlazerCommandLineOptions > options )
81+ private static int ProcessCompress ( CommandLineParser < BlazerCommandLineOptions > options )
6882 {
6983 var opt = options . Get ( ) ;
7084
7185 var fileName = options . GetNonParamOptions ( 0 ) ?? string . Empty ;
86+ string archiveName = null ;
87+
88+ string customFileName = null ;
89+
90+ var listFile = options . GetNonParamOptions ( ) . FirstOrDefault ( x => x [ 0 ] == '@' ) ;
91+ if ( listFile != null )
92+ {
93+ listFile = listFile . Remove ( 0 , 1 ) ;
94+ if ( ! File . Exists ( listFile ) )
95+ {
96+ Console . Error . WriteLine ( "Invalid list file" ) ;
97+ return 1 ;
98+ }
99+
100+ archiveName = fileName ;
101+ // currently we support only one file
102+ fileName = File . ReadAllLines ( listFile ) . FirstOrDefault ( ) ;
103+ }
72104
73105 if ( ! opt . Stdin && ! File . Exists ( fileName ) )
74106 {
75107 if ( fileName == string . Empty )
76108 {
77109 Console . WriteLine ( options . GenerateHelp ( ) ) ;
78- return ;
110+ return 0 ;
79111 }
80112
81113 Console . Error . WriteLine ( "Source file " + fileName + " does not exist" ) ;
82- return ;
114+ return 1 ;
83115 }
84116
85- var archiveName = fileName + ".blz" ;
117+ if ( archiveName == null )
118+ archiveName = fileName + ".blz" ;
86119 var truncateOutFile = false ;
87120 if ( ! opt . Stdout && File . Exists ( archiveName ) )
88121 {
89122 if ( ! opt . Force )
90123 {
91124 Console . WriteLine ( "Archive already exists. Overwrite? (Y)es (N)o" ) ;
92125 var readLine = Console . ReadLine ( ) ;
93- if ( readLine . Trim ( ) . ToLowerInvariant ( ) . IndexOf ( 'y' ) != 0 ) return ;
126+ if ( readLine . Trim ( ) . ToLowerInvariant ( ) . IndexOf ( 'y' ) != 0 ) return 1 ;
94127 }
95128
96129 truncateOutFile = true ;
97130 }
98131
99132 var mode = ( opt . Mode ?? "block" ) . ToLowerInvariant ( ) ;
100133
101-
102134 BlazerCompressionOptions compressionOptions = BlazerCompressionOptions . CreateStream ( ) ;
103135 compressionOptions . Password = opt . Password ;
104136 compressionOptions . EncryptFull = opt . EncryptFull ;
137+ compressionOptions . Comment = opt . Comment ;
105138
106139 if ( ! opt . NoFileName )
107- compressionOptions . FileInfo = BlazerFileInfo . FromFileName ( fileName ) ;
140+ compressionOptions . FileInfo = BlazerFileInfo . FromFileName ( fileName , false ) ;
108141
109142 if ( mode == "none" )
110143 compressionOptions . SetEncoderByAlgorithm ( BlazerAlgorithm . NoCompress ) ;
@@ -140,24 +173,42 @@ private static void ProcessCompress(CommandLineParser<BlazerCommandLineOptions>
140173 {
141174 inFile . CopyTo ( outFile ) ;
142175 }
176+
177+ return 0 ;
143178 }
144179
145- private static void ProcessDecompress ( CommandLineParser < BlazerCommandLineOptions > options )
180+ private static int ProcessDecompress ( CommandLineParser < BlazerCommandLineOptions > options )
146181 {
147182 var opt = options . Get ( ) ;
148183
149184 var archiveName = options . GetNonParamOptions ( 0 ) ?? string . Empty ;
150185
186+ string customOutFileName = null ;
187+
188+ var listFile = options . GetNonParamOptions ( ) . FirstOrDefault ( x => x [ 0 ] == '@' ) ;
189+ if ( listFile != null )
190+ {
191+ listFile = listFile . Remove ( 0 , 1 ) ;
192+ if ( ! File . Exists ( listFile ) )
193+ {
194+ Console . Error . WriteLine ( "Invalid list file" ) ;
195+ return 1 ;
196+ }
197+
198+ // currently we support only one file
199+ customOutFileName = File . ReadAllLines ( listFile ) . FirstOrDefault ( ) ;
200+ }
201+
151202 if ( ! opt . Stdin && ! File . Exists ( archiveName ) )
152203 {
153204 if ( archiveName == string . Empty )
154205 {
155206 Console . WriteLine ( options . GenerateHelp ( ) ) ;
156- return ;
207+ return 0 ;
157208 }
158209
159210 Console . Error . WriteLine ( "Archive file " + archiveName + " does not exist" ) ;
160- return ;
211+ return 1 ;
161212 }
162213
163214 Stream inStreamSource = opt . Stdin ? Console . OpenStandardInput ( ) : File . OpenRead ( archiveName ) ;
@@ -195,13 +246,15 @@ private static void ProcessDecompress(CommandLineParser<BlazerCommandLineOptions
195246 applyFileInfoAfterComplete = true ;
196247 }
197248
249+ if ( customOutFileName != null ) fileName = customOutFileName ;
250+
198251 if ( ! opt . Stdout && File . Exists ( fileName ) )
199252 {
200253 if ( ! opt . Force )
201254 {
202255 Console . WriteLine ( "Target " + fileName + " already exists. Overwrite? (Y)es (N)o" ) ;
203256 var readLine = Console . ReadLine ( ) ;
204- if ( readLine . Trim ( ) . ToLowerInvariant ( ) . IndexOf ( 'y' ) != 0 ) return ;
257+ if ( readLine . Trim ( ) . ToLowerInvariant ( ) . IndexOf ( 'y' ) != 0 ) return 1 ;
205258 }
206259
207260 new FileStream ( fileName , FileMode . Truncate , FileAccess . Write ) . Close ( ) ;
@@ -214,9 +267,11 @@ private static void ProcessDecompress(CommandLineParser<BlazerCommandLineOptions
214267 }
215268
216269 if ( applyFileInfoAfterComplete ) outStream . FileInfo . ApplyToFile ( ) ;
270+
271+ return 0 ;
217272 }
218273
219- private static void ProcessTest ( CommandLineParser < BlazerCommandLineOptions > options )
274+ private static int ProcessTest ( CommandLineParser < BlazerCommandLineOptions > options )
220275 {
221276 // todo: refactor. decompress method is similar
222277 var opt = options . Get ( ) ;
@@ -228,11 +283,11 @@ private static void ProcessTest(CommandLineParser<BlazerCommandLineOptions> opti
228283 if ( archiveName == string . Empty )
229284 {
230285 Console . WriteLine ( options . GenerateHelp ( ) ) ;
231- return ;
286+ return 0 ;
232287 }
233288
234289 Console . Error . WriteLine ( "Archive file " + archiveName + " does not exist" ) ;
235- return ;
290+ return 1 ;
236291 }
237292
238293 Stream inStreamSource = opt . Stdin ? Console . OpenStandardInput ( ) : File . OpenRead ( archiveName ) ;
@@ -259,7 +314,6 @@ private static void ProcessTest(CommandLineParser<BlazerCommandLineOptions> opti
259314
260315 var outStream = new BlazerOutputStream ( inStreamSource , decOptions ) ;
261316
262-
263317 using ( var inFile = outStream )
264318 using ( var outFile = new StatStream ( new NullStream ( ) , true ) )
265319 {
@@ -268,6 +322,91 @@ private static void ProcessTest(CommandLineParser<BlazerCommandLineOptions> opti
268322
269323 Console . WriteLine ( ) ;
270324 Console . WriteLine ( "File is correct" ) ;
325+
326+ return 0 ;
327+ }
328+
329+ private static int ProcessList ( CommandLineParser < BlazerCommandLineOptions > options )
330+ {
331+ // todo: refactor. decompress method is similar
332+ var opt = options . Get ( ) ;
333+
334+ var archiveName = options . GetNonParamOptions ( 0 ) ?? string . Empty ;
335+
336+ if ( ! opt . Stdin && ! File . Exists ( archiveName ) )
337+ {
338+ if ( archiveName == string . Empty )
339+ {
340+ Console . WriteLine ( options . GenerateHelp ( ) ) ;
341+ return 0 ;
342+ }
343+
344+ Console . Error . WriteLine ( "Archive file " + archiveName + " does not exist" ) ;
345+ return 1 ;
346+ }
347+
348+ Stream inStreamSource = opt . Stdin ? Console . OpenStandardInput ( ) : File . OpenRead ( archiveName ) ;
349+
350+ var decOptions = new BlazerDecompressionOptions ( opt . Password ) { EncyptFull = opt . EncryptFull } ;
351+
352+ if ( opt . BlobOnly )
353+ {
354+ decOptions . CompressionOptions = new BlazerCompressionOptions
355+ {
356+ IncludeCrc = false ,
357+ IncludeFooter = false ,
358+ IncludeHeader = false ,
359+ FileInfo = null ,
360+ MaxBlockSize = 1 << 24
361+ } ;
362+
363+ var mode = ( opt . Mode ?? "block" ) . ToLowerInvariant ( ) ;
364+ if ( mode == "stream" || mode == "streamhigh" ) decOptions . SetDecoderByAlgorithm ( BlazerAlgorithm . Stream ) ;
365+ else if ( mode == "none" ) decOptions . SetDecoderByAlgorithm ( BlazerAlgorithm . NoCompress ) ;
366+ else if ( mode == "block" ) decOptions . SetDecoderByAlgorithm ( BlazerAlgorithm . Block ) ;
367+ else throw new InvalidOperationException ( "Unsupported mode" ) ;
368+ }
369+
370+ // format is simplified 7z output
371+ using ( var outStream = new BlazerOutputStream ( inStreamSource , decOptions ) )
372+ {
373+ Console . WriteLine ( "Listing archive: " + ( opt . Stdin ? "stdin" : archiveName ) ) ;
374+ Console . WriteLine ( "Method: " + outStream . Algorithm ) ;
375+ Console . WriteLine ( "Max block size: " + outStream . MaxUncompressedBlockSize ) ;
376+ if ( outStream . Comment != null )
377+ {
378+ Console . WriteLine ( "Comment: " + outStream . Comment ) ;
379+ }
380+
381+ Console . WriteLine ( ) ;
382+ var fi = outStream . FileInfo ;
383+ if ( fi == null )
384+ {
385+ Console . WriteLine ( "Missing file information in archive." ) ;
386+ return 1 ;
387+ }
388+ else
389+ {
390+ Console . WriteLine ( " Date Time Attr Size Name" ) ;
391+ Console . WriteLine ( "------------------- ----- ------------ ------------------------" ) ;
392+ Console . WriteLine (
393+ "{0:yyyy-MM-dd} {1:HH:mm:ss} {2}{3}{4}{5}{6} {7,12} {8}" ,
394+ fi . CreationTimeUtc . ToLocalTime ( ) ,
395+ fi . CreationTimeUtc . ToLocalTime ( ) ,
396+ ( fi . Attributes & FileAttributes . Directory ) != 0 ? "D" : "." ,
397+ ( fi . Attributes & FileAttributes . ReadOnly ) != 0 ? "R" : "." ,
398+ ( fi . Attributes & FileAttributes . Hidden ) != 0 ? "H" : "." ,
399+ ( fi . Attributes & FileAttributes . System ) != 0 ? "S" : "." ,
400+ ( fi . Attributes & FileAttributes . Archive ) != 0 ? "A" : "." ,
401+ fi . Length ,
402+ fi . FileName ) ;
403+ Console . WriteLine ( "------------------- ----- ------------ ------------------------" ) ;
404+ // now, we have only one file, so there are no sense to write total
405+ // 4854 1018 2 files, 1 folders
406+ }
407+ }
408+
409+ return 0 ;
271410 }
272411
273412 private static Assembly CurrentDomainOnAssemblyResolve ( object sender , ResolveEventArgs args )
0 commit comments