Skip to content

Commit

Permalink
+ Closed #2: Added rough possibility to search through inner archives
Browse files Browse the repository at this point in the history
# bugfixes
  • Loading branch information
Nockiro committed Aug 12, 2017
1 parent f966e96 commit 2d6dc73
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 18 deletions.
3 changes: 3 additions & 0 deletions PackedFileSearcher/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<setting name="SearchInDirs" serializeAs="String">
<value>False</value>
</setting>
<setting name="RecursiveArchiveDepth" serializeAs="String">
<value>0</value>
</setting>
</PackedFileSearcher.Properties.Settings>
<ZipFileSearcher.Properties.Settings>
<setting name="UseWholePathForFileNames" serializeAs="String">
Expand Down
31 changes: 29 additions & 2 deletions PackedFileSearcher/FrmSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions PackedFileSearcher/FrmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ public FrmSettings()

cb_useWholeFilePathForName.Checked = Properties.Settings.Default.UseWholePathForFileNames;
cb_includeDirs.Checked = Properties.Settings.Default.SearchInDirs;
num_archiveDepth.Value = Properties.Settings.Default.RecursiveArchiveDepth;
}

private void cb_useWholeFilePathForName_CheckedChanged(object sender, EventArgs e)=> Properties.Settings.Default.UseWholePathForFileNames = cb_useWholeFilePathForName.Checked;
private void FrmSettings_FormClosing(object sender, FormClosingEventArgs e) => Properties.Settings.Default.Save();

#region value change

private void cb_useWholeFilePathForName_CheckedChanged(object sender, EventArgs e) => Properties.Settings.Default.UseWholePathForFileNames = cb_useWholeFilePathForName.Checked;

private void cb_includeDirs_Click(object sender, EventArgs e) => Properties.Settings.Default.SearchInDirs = cb_includeDirs.Checked;

~FrmSettings() => Properties.Settings.Default.Save();
private void num_archiveDepth_ValueChanged(object sender, EventArgs e) => Properties.Settings.Default.RecursiveArchiveDepth = num_archiveDepth.Value;

#endregion
}
}
4 changes: 2 additions & 2 deletions PackedFileSearcher/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// übernehmen, indem Sie "*" eingeben:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
12 changes: 12 additions & 0 deletions PackedFileSearcher/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PackedFileSearcher/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<Setting Name="SearchInDirs" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="RecursiveArchiveDepth" Type="System.Decimal" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
3 changes: 2 additions & 1 deletion PackedFileSearcher/Searchers/ISearcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public interface ISearcher
/// </summary>
/// <param name="paths">Search path</param>
/// <param name="pattern">Search string</param>
/// <param name="depth">Current depth of archive extraction</param>
/// <returns>List of all SearchResults</returns>
List<SearchResultInstance> Search(string pattern);
List<SearchResultInstance> Search(string pattern, int depth = 0);

/// <summary>
/// Extracts a found entry
Expand Down
29 changes: 25 additions & 4 deletions PackedFileSearcher/Searchers/SevenZipSearcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using SevenZip;
using PackedFileSearcher.Enums;
using SevenZip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -45,8 +47,9 @@ public ISearcher WithPath(string path)
/// Search in the given file for the given pattern
/// </summary>
/// <param name="pattern">String to be searched for</param>
/// <param name="depth">Current depth of archive extraction</param>
/// <returns>List with search results of entries</returns>
public List<SearchResultInstance> Search(string pattern)
public List<SearchResultInstance> Search(string pattern, int depth = 0)
{
List<SearchResultInstance> MatchingEntries = new List<SearchResultInstance>();

Expand All @@ -59,8 +62,26 @@ public List<SearchResultInstance> Search(string pattern)
foreach (ArchiveFileInfo entry in extr.ArchiveFileData)
{

if (Regex.IsMatch(entry.FileName, Utils.WildCardToRegular(pattern)) && (!entry.IsDirectory || Properties.Settings.Default.SearchInDirs))
// if either the file name matches the pattern or, if SearchInDirs is enabled, the path includes the pattern somewhere
if (Regex.IsMatch(System.IO.Path.GetFileName(entry.FileName), Utils.WildCardToRegular(pattern)) &&
(!entry.IsDirectory || (entry.IsDirectory && Properties.Settings.Default.SearchInDirs && Regex.IsMatch(entry.FileName, Utils.WildCardToRegular(pattern)))))
MatchingEntries.Add(new SearchResultInstance(this, Path, entry.FileName, System.IO.Path.GetFileName(entry.FileName), entry.Size, entry.LastWriteTime, entry.IsDirectory));

// if the current entry is an archive, check if we have a searcher for it and search through it to the depth given in the settings
if (Properties.Settings.Default.RecursiveArchiveDepth > 0 && SearcherTypeHelper.ExtensionToSearcherType(System.IO.Path.GetExtension(entry.FileName)) != SearcherType.None)
{
string tempFileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileNameWithoutExtension(Path), entry.FileName);

using (FileStream archiveStream = File.Create(tempFileName))
{
extr.ExtractFile(tempFileName, archiveStream);
ISearcher tempSearcher = Searcher.GetSearcher(SearcherTypeHelper.ExtensionToSearcherType(System.IO.Path.GetExtension(entry.FileName)));

foreach (SearchResultInstance si in tempSearcher.WithPath(tempFileName).Search(pattern, depth + 1))
MatchingEntries.Add(si);
}

}
}


Expand All @@ -85,7 +106,7 @@ public bool extract(SearchResultInstance s, string savePath)
{
SevenZipBase.SetLibraryPath("3rdParty" + System.IO.Path.DirectorySeparatorChar + "7z.dll");
using (SevenZipExtractor extr = new SevenZipExtractor(Path))
extr.ExtractFiles(savePath, s.FolderPath);
extr.ExtractFiles(savePath, s.FolderPath);

return true;
}
Expand Down
27 changes: 20 additions & 7 deletions PackedFileSearcher/Searchers/ZipFileSearcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PackedFileSearcher.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
Expand Down Expand Up @@ -43,8 +44,9 @@ public ISearcher WithPath(string path)
/// Search in the given file for the given pattern
/// </summary>
/// <param name="pattern">String to be searched for</param>
/// <param name="depth">Current recursion depth</param>
/// <returns>List with search results of entries</returns>
public List<SearchResultInstance> Search(string pattern)
public List<SearchResultInstance> Search(string pattern, int depth = 0)
{
List<SearchResultInstance> MatchingEntries = new List<SearchResultInstance>();

Expand All @@ -54,15 +56,26 @@ public List<SearchResultInstance> Search(string pattern)

foreach (ZipArchiveEntry entry in zip.GetRawEntries())
{
Boolean isDir = false;

if (Regex.IsMatch(entry.FullName, Utils.WildCardToRegular(pattern)))
if (entry.FullName.EndsWith("/") && entry.Name == "")
isDir = true;

// if either the file name matches the pattern or, if SearchInDirs is enabled, the path includes the pattern somewhere
if (Regex.IsMatch(entry.Name, Utils.WildCardToRegular(pattern)) || (isDir && Properties.Settings.Default.SearchInDirs && Regex.IsMatch(entry.FullName, Utils.WildCardToRegular(pattern))))
MatchingEntries.Add(new SearchResultInstance(this, Path, entry.FullName, entry.Name, (ulong)entry.Length, entry.LastWriteTime, isDir));

// if the current entry is an archive, check if we have a searcher for it and search through it to the depth given in the settings
if (Properties.Settings.Default.RecursiveArchiveDepth > 0 && SearcherTypeHelper.ExtensionToSearcherType(System.IO.Path.GetExtension(entry.Name)) != SearcherType.None)
{
Boolean isDir = false;
string tempFileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetFileNameWithoutExtension(Path), entry.Name);
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(tempFileName));

if (entry.FullName.EndsWith("/") && entry.Name == "")
isDir = true;
entry.ExtractToFile(tempFileName, true);
ISearcher tempSearcher = Searcher.GetSearcher(SearcherTypeHelper.ExtensionToSearcherType(System.IO.Path.GetExtension(entry.Name)));

MatchingEntries.Add(new SearchResultInstance(this, Path, entry.FullName, entry.Name, (ulong)entry.Length, entry.LastWriteTime, isDir));
foreach (SearchResultInstance si in tempSearcher.WithPath(tempFileName).Search(pattern, depth + 1))
MatchingEntries.Add(si);
}
}

Expand Down

0 comments on commit 2d6dc73

Please sign in to comment.