Skip to content

Commit 0e60d38

Browse files
committed
add basic support for wildcards in list file (compatible with far), small bug fixes
1 parent 54b3d68 commit 0e60d38

File tree

5 files changed

+65
-20
lines changed

5 files changed

+65
-20
lines changed

Blazer.Exe/Program.cs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
56
using System.Text;
7+
using System.Text.RegularExpressions;
68

79
using Force.Blazer.Algorithms;
810
using 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
}

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.9.0.0")]
35-
[assembly: AssemblyFileVersion("0.9.1.9")]
35+
[assembly: AssemblyFileVersion("0.9.2.10")]

Blazer.Net/Blazer.Net.nuspec

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>Blazer.Net</id>
55
<title>Blazer.Net</title>
6-
<version>0.9.0</version>
6+
<version>0.9.2</version>
77
<authors>force</authors>
88
<owners>force</owners>
99
<licenseUrl>https://github.com/force-net/blazer/blob/develop/LICENSE</licenseUrl>
@@ -20,11 +20,9 @@
2020
See project site for detailed information.
2121
</description>
2222
<releaseNotes>
23-
Fixed bugs
24-
Added some helper methods for compression
25-
Improved Crc32C api to use it in external applications if needed (instead of separate library)
26-
NoSeek option for decryption
27-
Mega-feature: patterned compression, which can reduce compressed size of similar small messages
23+
StreamHigh optimized and its compression rate slightly improved.
24+
Added MaxBlockSize option from flags.
25+
Added support for multiple files in one archive.
2826
</releaseNotes>
2927
<copyright>Copyright by Force 2016</copyright>
3028
<tags>.NET fast compression archive crc32c</tags>

Blazer.Net/BlazerFileInfo.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,18 @@ public static BlazerFileInfo FromFileName(string fileName, bool leaveFullName)
6868
/// </summary>
6969
public void ApplyToFile()
7070
{
71-
if (!File.Exists(FileName) && !Directory.Exists(FileName))
72-
return;
73-
File.SetAttributes(FileName, Attributes);
74-
File.SetCreationTimeUtc(FileName, CreationTimeUtc);
75-
File.SetLastWriteTimeUtc(FileName, CreationTimeUtc);
71+
if (File.Exists(FileName))
72+
{
73+
File.SetAttributes(FileName, Attributes);
74+
File.SetCreationTimeUtc(FileName, CreationTimeUtc);
75+
File.SetLastWriteTimeUtc(FileName, LastWriteTimeUtc);
76+
}
77+
else if (Directory.Exists(FileName))
78+
{
79+
new DirectoryInfo(FileName).Attributes = Attributes;
80+
Directory.SetCreationTimeUtc(FileName, CreationTimeUtc);
81+
Directory.SetLastWriteTimeUtc(FileName, LastWriteTimeUtc);
82+
}
7683
}
7784
}
7885
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Extract=Blazer -d {-p%%P} -f %%AQ @%%LQMN
120120
ExtractWithoutPath=Blazer -d {-p%%P} --nopathname -f %%AQ @%%LQMN
121121
Test=Blazer -t {-p%%P} %%AQ @%%LQMN
122122
Add=Blazer {-p%%P} %%AQ @%%LQMN
123+
AddRecurse=Blazer {-p%%P} %%AQ @%%LQMN
123124
AllFilesMask="*"
124125
125126
```
@@ -139,6 +140,7 @@ Blazer compresses data into stream with special format and with variety of setti
139140
* Ability to compress to stdout and read data from stdin (no seeks in stream, if it does not support it)
140141
* Ability to use non-compressed data in same structure
141142
* **[Compression with pattern](Doc/PatternedCompression.md)**
143+
* Archive with one or multiple files (command-line utility has only basic support for multi-file archives).
142144

143145
## Implementation
144146

0 commit comments

Comments
 (0)