Skip to content

Commit 863d677

Browse files
Max file size (#24)
* Max file size setting added * Remove unused using Co-authored-by: CommonLoon102 <[email protected]>
1 parent fbe3855 commit 863d677

File tree

7 files changed

+63
-34
lines changed

7 files changed

+63
-34
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace RegexFileSearcher
6+
{
7+
internal static class Extensions
8+
{
9+
public static IEnumerable<FilePath> NotZippedFiles(this IEnumerable<FilePath> filePaths)
10+
{
11+
return filePaths.Where(fp => !IsZipFile(fp.Path));
12+
}
13+
14+
public static bool IsZipFile(this string fileName)
15+
{
16+
string extension = Path.GetExtension(fileName).ToLower();
17+
return extension == ".zip";
18+
}
19+
}
20+
}

RegexFileSearcher/RegexFileSearcher/MainForm.init.xeto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public partial class MainForm : Form
1616
private readonly CheckBox chkRightToLeft;
1717
private readonly CheckBox chkSingleLine;
1818
private readonly CheckBox chkSearchInZipFiles;
19-
private readonly NumericStepper nudTimeout;
19+
private readonly NumericStepper nudMaxFileSize;
2020
private readonly TextBox txtContentRegex;
2121
private readonly CheckBox chkContentCompiled;
2222
private readonly CheckBox chkContentCultureInvariant;
@@ -55,7 +55,7 @@ private MainForm(bool initializeControls)
5555
chkRightToLeft = FindChild<CheckBox>("chkRightToLeft");
5656
chkSingleLine = FindChild<CheckBox>("chkSingleLine");
5757
chkSearchInZipFiles = FindChild<CheckBox>("chkSearchInZipFiles");
58-
nudTimeout = FindChild<NumericStepper>("nudTimeout");
58+
nudMaxFileSize = FindChild<NumericStepper>("nudMaxFileSize");
5959
txtContentRegex = FindChild<TextBox>("txtContentRegex");
6060
chkContentCompiled = FindChild<CheckBox>("chkContentCompiled");
6161
chkContentCultureInvariant = FindChild<CheckBox>("chkContentCultureInvariant");

RegexFileSearcher/RegexFileSearcher/MainForm.xeto

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@
4242
</TableCell>
4343
<TableCell>
4444
<StackLayout Orientation="Vertical" Padding="10,0,0,0">
45-
<CheckBox ID="chkSearchInZipFiles" ToolTip="Search in zip files recursively">Search in .zip Files</CheckBox>
45+
<CheckBox ID="chkSearchInZipFiles" Checked="True" ToolTip="Search in zip files recursively">Search in .zip Files</CheckBox>
4646
<StackLayout VerticalContentAlignment="Center" ToolTip="Stop search after specified seconds" Orientation="Horizontal">
47-
<Label>Timeout</Label>
47+
<Label ToolTip="Skip files that are larger than the specified size (zip files are handled like they were folders)">Max File Size</Label>
4848
<Panel Padding="5,0,5,0">
49-
<NumericStepper ID="nudTimeout"
50-
MinValue="1"
51-
MaxValue="100"
49+
<NumericStepper ID="nudMaxFileSize"
50+
MinValue="0"
51+
MaxValue="1024"
5252
MaximumDecimalPlaces="0"
53-
Value="2"
53+
Value="5"
5454
Increment="1"
55-
Width="90" />
55+
Width="50"
56+
ToolTip="Use zero for unlimited" />
5657
</Panel>
57-
<Label>s</Label>
58+
<Label ToolTip="1 MiB (mebibyte) is 1024*1024 bytes">MiB</Label>
5859
</StackLayout>
5960
</StackLayout>
6061
</TableCell>

RegexFileSearcher/RegexFileSearcher/MainForm.xeto.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace RegexFileSearcher
1313
{
1414
public partial class MainForm : Form
1515
{
16+
private const int FileNameRegexTimeoutInSeconds = 5;
17+
1618
private readonly TreeGridItemCollection _itemCollection = new TreeGridItemCollection();
1719

1820
private CancellationTokenSource _cancellationTokenSource;
@@ -60,7 +62,7 @@ static bool ValidateRegex(TextBox textBox)
6062
IsMultiline = chkMultiline.Checked ?? false,
6163
IsRightToLeft = chkRightToLeft.Checked ?? false,
6264
IsSingleLine = chkSingleLine.Checked ?? false,
63-
Timeout = (int)nudTimeout.Value
65+
TimeoutInSeconds = FileNameRegexTimeoutInSeconds
6466
}.Regex;
6567

6668
private Regex ContentRegex =>
@@ -77,14 +79,15 @@ static bool ValidateRegex(TextBox textBox)
7779
IsMultiline = chkContentMultiline.Checked ?? false,
7880
IsRightToLeft = chkContentRightToLeft.Checked ?? false,
7981
IsSingleLine = chkContentSingleLine.Checked ?? false,
80-
Timeout = (int)nudContentTimeout.Value
82+
TimeoutInSeconds = (int)nudContentTimeout.Value
8183
}.Regex;
8284

8385
private RegexSearcher CreateNewSearcher()
8486
{
8587
return new RegexSearcher(fpSearchPath.FilePath,
8688
SearchDepth,
8789
chkSearchInZipFiles.Checked ?? false,
90+
(int)nudMaxFileSize.Value * 1024 * 1024,
8891
FileNameRegex,
8992
ContentRegex,
9093
_itemCollection,

RegexFileSearcher/RegexFileSearcher/RegexPattern.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ internal class RegexPattern
1515
public bool IsSingleLine { get; set; }
1616
public bool IsRightToLeft { get; set; }
1717
public bool IsCultureInvariant { get; set; }
18-
public int Timeout { get; set; }
18+
public int TimeoutInSeconds { get; set; }
1919

20-
public Regex Regex => new Regex(Pattern, RegexOptions, TimeSpan.FromSeconds(Timeout));
20+
public Regex Regex => new Regex(Pattern, RegexOptions, TimeSpan.FromSeconds(TimeoutInSeconds));
2121

2222
private RegexOptions RegexOptions
2323
{

RegexFileSearcher/RegexFileSearcher/RegexSearcher.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,20 @@ internal class RegexSearcher
1919
private readonly string _searchDirectory;
2020
private readonly bool _recurseSubdirectories;
2121
private readonly bool _searchInZipFiles;
22+
private readonly int _maxFileSize;
2223
private readonly Regex _fileNameRegex;
2324
private readonly Regex _contentRegex;
2425
private readonly CancellationToken _cancellationToken;
2526
private readonly TreeGridItemCollection _itemCollection;
27+
private readonly ZipFileWalker _zipFileWalker;
2628

2729
private string _currentDirectory;
2830

2931
// Waiting for init-only properties in C# 9
3032
public RegexSearcher(string searchDir,
3133
int depth,
3234
bool searchInZipFiles,
35+
int maxFileSize,
3336
Regex fileRegex,
3437
Regex contentRegex,
3538
TreeGridItemCollection itemCollection,
@@ -39,11 +42,13 @@ public RegexSearcher(string searchDir,
3942
_depth = depth;
4043
_recurseSubdirectories = depth < 0;
4144
_searchInZipFiles = searchInZipFiles;
45+
_maxFileSize = maxFileSize;
4246
_fileNameRegex = fileRegex;
4347
_contentRegex = contentRegex;
4448
_itemCollection = itemCollection;
4549
_cancellationToken = token;
4650
_searchEnded = false;
51+
_zipFileWalker = new ZipFileWalker(_maxFileSize);
4752
}
4853

4954
public string CurrentDirectory
@@ -113,29 +118,28 @@ private IEnumerable<IEnumerable<FilePath>> EnumerateFiles(string dir, int curren
113118
{
114119
// Although IgnoreInaccessible is true by default,
115120
// it only applies when you use the 3 parameter overload
116-
filePaths.AddRange(Directory.EnumerateFiles(dir, "*", _options).Select(f => new FilePath(f)));
121+
filePaths.AddRange(new DirectoryInfo(dir).EnumerateFiles("*", _options)
122+
.Where(fi => fi.Name.IsZipFile() || _maxFileSize == 0 || fi.Length <= _maxFileSize)
123+
.Select(fi => new FilePath(fi.FullName)));
117124
}
118125
catch
119126
{
120127
// IO exceptions e.g. directory was removed during enumeration
121128
}
122129

123-
yield return filePaths;
130+
yield return filePaths.NotZippedFiles();
124131

125132
// Any direcotry path exception has already been handled above
126133
foreach (var subDir in Directory.EnumerateDirectories(dir, "*", _options))
127134
{
128-
foreach (var subFiles in EnumerateFiles(subDir, currentDepth - 1))
129-
{
130-
yield return subFiles;
131-
}
135+
yield return EnumerateFiles(subDir, currentDepth - 1).SelectMany(f => f);
132136
}
133137

134138
if (_searchInZipFiles)
135139
{
136140
foreach (FilePath filePath in filePaths)
137141
{
138-
yield return ZipFileWalker.GetZippedFiles(filePath);
142+
yield return _zipFileWalker.GetZippedFiles(filePath);
139143
}
140144
}
141145
}

RegexFileSearcher/RegexFileSearcher/ZipFileWalker.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55

66
namespace RegexFileSearcher
77
{
8-
public static class ZipFileWalker
8+
internal class ZipFileWalker
99
{
10-
public static IEnumerable<FilePath> GetZippedFiles(FilePath filePath)
10+
private readonly int _maxFileSize;
11+
12+
public ZipFileWalker(int maxFileSize)
13+
{
14+
_maxFileSize = maxFileSize;
15+
}
16+
17+
public IEnumerable<FilePath> GetZippedFiles(FilePath filePath)
1118
{
1219
var results = new List<FilePath>();
13-
if (!IsZipFile(filePath.Path))
20+
if (!filePath.Path.IsZipFile())
1421
{
1522
return results;
1623
}
@@ -30,7 +37,7 @@ public static IEnumerable<FilePath> GetZippedFiles(FilePath filePath)
3037
return results;
3138
}
3239

33-
private static IEnumerable<FilePath> GetCompressedFilesInner(FilePath parentFilePath, Stream zipStream)
40+
private IEnumerable<FilePath> GetCompressedFilesInner(FilePath parentFilePath, Stream zipStream)
3441
{
3542
var results = new List<FilePath>();
3643
try
@@ -39,7 +46,7 @@ private static IEnumerable<FilePath> GetCompressedFilesInner(FilePath parentFile
3946
foreach (ZipEntry zipEntry in GetZipEntries(zipFile))
4047
{
4148
string zipEntryName = zipEntry.Name;
42-
if (IsZipFile(zipEntryName))
49+
if (zipEntryName.IsZipFile())
4350
{
4451
Stream entryStream = null;
4552
try
@@ -72,21 +79,15 @@ private static IEnumerable<FilePath> GetCompressedFilesInner(FilePath parentFile
7279
return results;
7380
}
7481

75-
private static IEnumerable<ZipEntry> GetZipEntries(ZipFile zipFile)
82+
private IEnumerable<ZipEntry> GetZipEntries(ZipFile zipFile)
7683
{
7784
foreach (ZipEntry zipEntry in zipFile)
7885
{
79-
if (zipEntry.IsFile)
86+
if (zipEntry.IsFile && (_maxFileSize == 0 || zipEntry.Size <= _maxFileSize))
8087
{
8188
yield return zipEntry;
8289
}
8390
}
8491
}
85-
86-
private static bool IsZipFile(string fileName)
87-
{
88-
string extension = Path.GetExtension(fileName).ToLower();
89-
return extension == ".zip";
90-
}
9192
}
9293
}

0 commit comments

Comments
 (0)