/// <summary> /// Checks the given files if they are compatible archive types and adds them /// </summary> /// <param name="files">files to add</param> /// <returns>Reading Status: Status code whether the search threw an error or if a cancellation request was made</returns> private ReadingStatus addFilesToSearchList(params string[] files) { List <ListViewItem> filesToAdd = new List <ListViewItem>(); foreach (string f in files) { // if we have a cancellation request and the worker is still working, we grant it if (bw_loadFiles.IsBusy && bw_loadFiles.CancellationPending) { // Get all by now processed items and add them all at once - addrange is a lot faster than adding them seperately this.InvokeIfRequired(() => lv_files.Items.AddRange(filesToAdd.ToArray())); // report a cancellation request return(ReadingStatus.CancelRequest); } // .. if we have a searcher for the file, we process it - otherwise, we don't if (SearcherTypeHelper.GetSearcherFromPath(f) != SearcherType.None) { this.InvokeIfRequired(() => setStatus($"Read files.. Valid: { f }", true, true)); // Process file path into a new listviewitem and add it to list of "items to be added" filesToAdd.Add(processFilePath(f)); } } // Get all by now processed items and add them all at once - addrange is a lot faster than adding them seperately this.InvokeIfRequired(() => lv_files.Items.AddRange(filesToAdd.ToArray())); return(ReadingStatus.OK); }
/// <summary> /// Turns file paths into listview items /// </summary> /// <param name="filePath">whole path to the file (eg. C:\reward\sink.jpg)</param> /// <returns></returns> private ListViewItem processFilePath(string filePath) { // Set the path's file name as first text to be seen on the item ListViewItem item = new ListViewItem(Path.GetFileName(filePath)); // Set only the path to the file, e.g C:\testFolder in the second column item.SubItems.Add(Path.GetDirectoryName(filePath)); // Set only the extension, e.g. ".zip" in the third column item.SubItems.Add(Path.GetExtension(filePath)); // Set the object to the list item as we could need it later to extract files or show further information item.Tag = Searcher.GetSearcher(SearcherTypeHelper.GetSearcherFromPath(filePath)).WithPath(filePath); return(item); }
/// <summary> /// 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, int depth = 0) { List <SearchResultInstance> MatchingEntries = new List <SearchResultInstance>(); try { using (ZipArchive zip = ZipFile.Open(Path, ZipArchiveMode.Read)) foreach (ZipArchiveEntry entry in zip.GetRawEntries()) { Boolean isDir = false; 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.GetSearcherFromPath(entry.Name) != SearcherType.None) { string tempFileName = System.IO.Path.Combine(Program.tempPath, System.IO.Path.GetFileNameWithoutExtension(Utils.NextAvailableFilename(Path, true)), entry.Name); Directory.CreateDirectory(System.IO.Path.GetDirectoryName(tempFileName)); entry.ExtractToFile(tempFileName, true); ISearcher tempSearcher = Searcher.GetSearcher(SearcherTypeHelper.GetSearcherFromPath(entry.Name)); foreach (SearchResultInstance si in tempSearcher.WithPath(tempFileName).Search(pattern, depth + 1)) { MatchingEntries.Add(si); } } } } catch (Exception) { Error = true; return(MatchingEntries); } return(MatchingEntries); }
/// <summary> /// 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, int depth = 0) { List <SearchResultInstance> MatchingEntries = new List <SearchResultInstance>(); try { SevenZipExtractor extr = new SevenZipExtractor(Path); foreach (ArchiveFileInfo entry in extr.ArchiveFileData) { // 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.GetSearcherFromPath(entry.FileName) != SearcherType.None) { string tempFileName = System.IO.Path.Combine(Program.tempPath, System.IO.Path.GetFileNameWithoutExtension(Utils.NextAvailableFilename(Path, true)), entry.FileName); using (FileStream archiveStream = File.Create(tempFileName)) { extr.ExtractFile(tempFileName, archiveStream); ISearcher tempSearcher = Searcher.GetSearcher(SearcherTypeHelper.GetSearcherFromPath(entry.FileName)); foreach (SearchResultInstance si in tempSearcher.WithPath(tempFileName).Search(pattern, depth + 1)) { MatchingEntries.Add(si); } } } } } catch (Exception) { Error = true; return(MatchingEntries); } return(MatchingEntries); }