Skip to content

Commit cc0ccdb

Browse files
committed
changed path handling in archive, added comment support in archive, added list command, basic integration with far, bug fixes
1 parent e12d5d6 commit cc0ccdb

File tree

14 files changed

+378
-55
lines changed

14 files changed

+378
-55
lines changed

Blazer.Exe/CommandLine/BlazerCommandLineOptions.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ public class BlazerCommandLineOptions
88
[CommandLineOption('d', "decompress", "Decompress archive")]
99
public bool Decompress { get; set; }
1010

11+
[CommandLineOption('l', "list", "List content of archive")]
12+
public bool List { get; set; }
13+
14+
[CommandLineOption('t', "test", "Test archive")]
15+
public bool Test { get; set; }
16+
1117
[CommandLineOption('f', "force", "Overwrite target files without confirmation")]
1218
public bool Force { get; set; }
1319

@@ -32,7 +38,7 @@ public class BlazerCommandLineOptions
3238
[CommandLineOption("blobonly", "Compress to blob (no header and footer)")]
3339
public bool BlobOnly { get; set; }
3440

35-
[CommandLineOption('t', "test", "Test archive")]
36-
public bool Test { get; set; }
41+
[CommandLineOption("comment", "Add comment to archive")]
42+
public string Comment { get; set; }
3743
}
3844
}

Blazer.Exe/CommandLine/CommandLineParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ public string GenerateHelp()
157157
return b.ToString();
158158
}
159159

160-
public string GenerateHeader()
160+
public string GenerateHeader(string additionalVersion)
161161
{
162162
var title = (AssemblyTitleAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), true).First();
163163
var copy = (AssemblyCopyrightAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true).First();
164164
var version = (AssemblyFileVersionAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).First();
165165

166-
return string.Format("{0} {2} {1}", title.Title, copy.Copyright, version.Version);
166+
return string.Format("{0} {2} ({3}) {1}", title.Title, copy.Copyright, version.Version, additionalVersion);
167167
}
168168
}
169169
}

Blazer.Exe/Program.cs

Lines changed: 164 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Reflection;
45

56
using 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)

Blazer.Exe/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
3434
[assembly: AssemblyVersion("0.8.0.0")]
35-
[assembly: AssemblyFileVersion("0.8.0.6")]
35+
[assembly: AssemblyFileVersion("0.8.1.7")]

0 commit comments

Comments
 (0)