From 8f2ae783524aab0304da5c16730efb833aa7f111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 00:09:58 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85=E6=96=87=E4=BB=B6=E5=86=85=E5=AE=B9=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=8F=AF=E9=80=89=E9=A1=B9=EF=BC=88=E9=BB=98=E8=AE=A4=E4=B8=8D?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=EF=BC=89=20=E4=BC=98=E5=8C=96=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=B6=88=E8=80=97=E6=97=B6=E9=97=B4=E5=8F=8B=E5=A5=BD?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=20=E4=BC=98=E5=8C=96=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E3=80=81=E4=BB=A3=E7=A0=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=8C=89=E8=A1=8C=E6=98=BE=E7=A4=BA=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=85=B3=E9=94=AE=E8=AF=8D=E9=AB=98=E4=BA=AE=E5=BF=BD=E7=95=A5?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Core/AppConst.cs | 12 ++- TextLocator/Core/TaskTime.cs | 31 ++++++-- TextLocator/Index/IndexCore.cs | 4 +- TextLocator/MainWindow.xaml.cs | 12 +-- TextLocator/Service/DevelopFileService.cs | 2 +- TextLocator/Service/DomFileService.cs | 2 +- TextLocator/Service/TxtFileService.cs | 2 +- TextLocator/Service/ZipFileService.cs | 95 ++++++++++++++++++++--- TextLocator/SettingWindow.xaml | 45 ++++++++--- TextLocator/SettingWindow.xaml.cs | 71 ++++++++++++----- TextLocator/Util/RichTextBoxUtil.cs | 18 +++-- 11 files changed, 225 insertions(+), 69 deletions(-) diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index 6ebd20c..a56bb1a 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -35,7 +35,11 @@ namespace TextLocator.Core /// <summary> /// 压缩包解析大小 /// </summary> - public static int ZIP_FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "ZipFileSizeLimit", "20000000")); + public static int ZIP_FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "ZipFileSizeLimit", "100000000")); + /// <summary> + /// 是否解析压缩包内容 + /// </summary> + public static bool IS_PARSE_ZIP_CONTENT = bool.Parse(AppUtil.ReadValue("AppConfig", "IsParseZipContent", "false")); /// <summary> /// 缓存池容量 /// </summary> @@ -47,6 +51,10 @@ namespace TextLocator.Core /// </summary> public static readonly string APP_INDEX_DIR = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Index"); /// <summary> + /// 临时目录:_AppDir\\_AppName\\Temp\\ + /// </summary> + public static readonly string APP_TEMP_DIR = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Temp"); + /// <summary> /// 分词器 /// new Lucene.Net.Analysis.Cn.ChineseAnalyzer(); /// new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);// 用standardAnalyzer分词器 @@ -71,7 +79,7 @@ namespace TextLocator.Core /// <summary> /// 匹配空白和换行 /// </summary> - public static readonly Regex REGEX_LINE_BREAKS_AND_WHITESPACE = new Regex(" |\r|\n|\\s"); + public static readonly Regex REGEX_LINE_BREAKS_AND_WHITESPACE = new Regex(" |\r|\n|┄|\\s"); /// <summary> /// 匹配HTML和XML标签 /// </summary> diff --git a/TextLocator/Core/TaskTime.cs b/TextLocator/Core/TaskTime.cs index 5bbfa4b..068f9c2 100644 --- a/TextLocator/Core/TaskTime.cs +++ b/TextLocator/Core/TaskTime.cs @@ -7,6 +7,11 @@ namespace TextLocator.Core /// </summary> public class TaskTime { + /// <summary> + /// 文件大小单位 + /// </summary> + private static readonly string[] suffixes = new string[] { " 秒", " 分", " 时" }; + /// <summary> /// 开始时间 /// </summary> @@ -21,14 +26,28 @@ namespace TextLocator.Core } /// <summary> - /// 消耗时间 + /// 消耗时间(友好显示) /// </summary> - /// <returns></returns> - public double ConsumeTime + public string ConsumeTime { - get - { - return (DateTime.Now - beginTime).TotalSeconds; + get { + double time = (DateTime.Now - beginTime).TotalMilliseconds; + if (time > 1000) + { + if (time / 1000 < 60) + { + return time / 1000 + " 秒"; + } + else if (time / 1000 / 60 < 60) + { + return time / 1000 / 60 + " 分"; + } + else if (time / 1000 / 60 / 60 < 24) + { + return time / 1000 / 60 / 60 + " 时"; + } + } + return time + " 毫秒"; } } diff --git a/TextLocator/Index/IndexCore.cs b/TextLocator/Index/IndexCore.cs index 826cb2f..d5c5a24 100644 --- a/TextLocator/Index/IndexCore.cs +++ b/TextLocator/Index/IndexCore.cs @@ -248,7 +248,7 @@ namespace TextLocator.Index // 文件内容 string content = FileInfoServiceFactory.GetFileContent(filePath); - msg.Append(",解析:" + taskMark.ConsumeTime + "秒"); + msg.Append(",解析:" + taskMark.ConsumeTime); // 判断文件内容 if (!string.IsNullOrEmpty(content)) { @@ -284,7 +284,7 @@ namespace TextLocator.Index // 执行删除、添加逻辑 AddDocument(filePath, doc); } - msg.Append(",索引:" + taskMark.ConsumeTime + "秒"); + msg.Append(",索引:" + taskMark.ConsumeTime); } else { diff --git a/TextLocator/MainWindow.xaml.cs b/TextLocator/MainWindow.xaml.cs index 9bf6ab4..f9d796e 100644 --- a/TextLocator/MainWindow.xaml.cs +++ b/TextLocator/MainWindow.xaml.cs @@ -176,7 +176,7 @@ namespace TextLocator { SortOptions.Items.Add(sort); } - log.Debug("InitializeSortType 耗时:" + taskTime.ConsumeTime + "秒"); + log.Debug("InitializeSortType 耗时:" + taskTime.ConsumeTime + "。"); } /// <summary> @@ -231,7 +231,7 @@ namespace TextLocator Background = Brushes.DarkGray }); } - log.Debug("InitializeFileTypeFilters 耗时:" + taskTime.ConsumeTime + "秒"); + log.Debug("InitializeFileTypeFilters 耗时:" + taskTime.ConsumeTime + "。"); } /// <summary> @@ -275,7 +275,7 @@ namespace TextLocator ExclusionPaths.Text = exclusionPaths; ExclusionPaths.ToolTip = ExclusionPaths.Text; - log.Debug("InitializeAppConfig 耗时:" + taskTime.ConsumeTime + "秒"); + log.Debug("InitializeAppConfig 耗时:" + taskTime.ConsumeTime + "。"); } #endregion @@ -587,7 +587,7 @@ namespace TextLocator this.SwitchPreview.Visibility = totalHits > 0 ? Visibility.Visible : Visibility.Hidden; })); - string msg = "检索完成。分词:( " + text + " ),结果:" + totalHits + "个符合条件的结果 (第 " + pageNow + " 页),耗时:" + taskMark.ConsumeTime + "秒。"; + string msg = "检索完成。分词:( " + text + " ),结果:" + totalHits + "个符合条件的结果 (第 " + pageNow + " 页),耗时:" + taskMark.ConsumeTime + "。"; log.Debug(msg); @@ -1162,7 +1162,7 @@ namespace TextLocator // 获取文件信息列表 FileUtil.GetAllFiles(filePaths, s, _regexExclusionFolder); } - log.Debug("GetFiles 耗时:" + fileMark.ConsumeTime + "秒"); + log.Debug("GetFiles 耗时:" + fileMark.ConsumeTime + "。"); ShowStatus("文件扫描完成,开始" + tips + "索引..."); // 验证扫描文件列表是否为空 @@ -1189,7 +1189,7 @@ namespace TextLocator // 创建索引方法 IndexCore.CreateIndex(filePaths, rebuild, ShowStatus); - string msg = "索引" + tips + "完成。共用时:" + taskMark.ConsumeTime + "秒"; + string msg = "索引" + tips + "完成。共用时:" + taskMark.ConsumeTime + "。"; // 显示状态 ShowStatus(msg); diff --git a/TextLocator/Service/DevelopFileService.cs b/TextLocator/Service/DevelopFileService.cs index 2e3d6b3..f8ab5ff 100644 --- a/TextLocator/Service/DevelopFileService.cs +++ b/TextLocator/Service/DevelopFileService.cs @@ -27,7 +27,7 @@ namespace TextLocator.Service string line; while ((line = reader.ReadLine()) != null) { - builder.Append(AppConst.REGEX_TAG.Replace(line, "")); + builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); } } } diff --git a/TextLocator/Service/DomFileService.cs b/TextLocator/Service/DomFileService.cs index 30c6d10..efc0a08 100644 --- a/TextLocator/Service/DomFileService.cs +++ b/TextLocator/Service/DomFileService.cs @@ -27,7 +27,7 @@ namespace TextLocator.Service string line; while((line = reader.ReadLine()) != null) { - builder.Append(AppConst.REGEX_TAG.Replace(line, "")); + builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); } } } diff --git a/TextLocator/Service/TxtFileService.cs b/TextLocator/Service/TxtFileService.cs index ebce225..876955f 100644 --- a/TextLocator/Service/TxtFileService.cs +++ b/TextLocator/Service/TxtFileService.cs @@ -25,7 +25,7 @@ namespace TextLocator.Service string line; while((line = reader.ReadLine()) != null) { - builder.Append(line); + builder.AppendLine(line); } } } diff --git a/TextLocator/Service/ZipFileService.cs b/TextLocator/Service/ZipFileService.cs index fda4e9d..3fbf2f0 100644 --- a/TextLocator/Service/ZipFileService.cs +++ b/TextLocator/Service/ZipFileService.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using TextLocator.Core; +using TextLocator.Enums; using TextLocator.Exceptions; using TextLocator.Factory; using TextLocator.Util; @@ -28,25 +29,45 @@ namespace TextLocator.Service // 内容 StringBuilder builder = new StringBuilder(); lock (locker) - { + { try { + // 文件信息 + FileInfo fileInfo = new FileInfo(filePath); + // 文件名称 + string fileName = fileInfo.Name; + // 文件大小 + long fileSize = fileInfo.Length; + // 压缩包解压 - builder.Append("名称:" + filePath.Substring(filePath.LastIndexOf("\\") + 1)); - builder.Append(" 大小:" + FileUtil.GetFileSizeFriendly(new FileInfo(filePath).Length) + " =>\r\n"); + builder.Append("名称:" + fileName); + builder.Append(" 大小:" + FileUtil.GetFileSizeFriendly(fileInfo.Length)); + builder.Append(" 列表:=> \r\n"); - builder.Append(" 列表:=>\r\n"); - using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + // 解析文件内容 && 文件大小 + if (AppConst.IS_PARSE_ZIP_CONTENT && fileSize <= AppConst.ZIP_FILE_SIZE_LIMIT) { - using (var archive = ArchiveFactory.Open(file)) { - foreach (var entry in archive.Entries) + // 获取文件信息,判断压缩包大小。大于限制大小的同样不解析文件内容 + string unzipPath = Path.Combine(AppConst.APP_TEMP_DIR, fileName); + + // 判断文件夹是否存在,不存在创建 + if (!Directory.Exists(unzipPath)) + { + try { - if (!entry.IsDirectory) - { - builder.Append(String.Format(" {0}, {1}\r\n", entry.Key, FileUtil.GetFileSizeFriendly(entry.Size))); - } + Directory.CreateDirectory(unzipPath); + } + catch (Exception ex) + { + log.Error("解压文件夹创建失败:" + ex.Message, ex); } } + + builder.Append(GetContent(filePath, unzipPath)); + } + else + { + builder.Append(GetContent(filePath, null)); } } catch (Exception ex) @@ -56,5 +77,57 @@ namespace TextLocator.Service } return builder.ToString(); } + + /// <summary> + /// 获取内容 + /// </summary> + /// <param name="filePath">压缩文件路径</param> + /// <param name="unzipPath">解压路径</param> + /// <param name="isParseContent">是否解析文件内容</param> + /// <returns></returns> + private string GetContent(string filePath, string unzipPath) + { + StringBuilder builder = new StringBuilder(); + using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + { + using (var archive = ArchiveFactory.Open(file)) + { + foreach (var entry in archive.Entries) + { + if (!entry.IsDirectory) + { + builder.Append("┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\r\n"); + builder.Append(string.Format("{0}, {1}\r\n", entry.Key, FileUtil.GetFileSizeFriendly(entry.Size))); + // 解压路径不为空,代表需要解压并解析 + if (!string.IsNullOrEmpty(unzipPath)) + { + // 获取文件类型 + FileType fileType = FileTypeUtil.GetFileType(entry.Key); + // 文件后缀 + string fileExt = Path.GetExtension(entry.Key); + if (!string.IsNullOrEmpty(fileExt)) + { + fileExt = fileExt.Substring(1); + } + // 支持的文件后缀集 + string fileExts = FileTypeUtil.GetFileTypeExts(); + // 不是压缩包 && 文件后缀在可解析范围 + if (fileType != FileType.压缩包 && fileExts.Contains(fileExt)) + { + // 判断文件是否支持解析,不支持解析的文件无需解压 + entry.WriteToDirectory(unzipPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); + + // 获取文件信息,判断压缩包大小。大于限制大小的同样不解析文件内容 + string unzipFile = Path.Combine(unzipPath, entry.Key); + // 解析文件内容 + builder.Append(FileInfoServiceFactory.GetFileContent(unzipFile) + "\r\n"); + } + } + } + } + } + } + return builder.ToString(); + } } } diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index 53a033c..c642dae 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -19,32 +19,45 @@ <Grid Margin="20,20,20,0"> <Grid HorizontalAlignment="Left"> <TextBlock Text="最小数量:" HorizontalAlignment="Left"/> - <TextBox x:Name="MinThreads" Text="32" ToolTip="建议:CPU核心数" HorizontalAlignment="Left" Margin="70,0,0,0" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> + <TextBox x:Name="MinThreads" Text="32" ToolTip="默认:32,建议:CPU核心数" HorizontalAlignment="Left" Margin="70,0,0,0" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> </Grid> </Grid> <Grid Margin="20,20,20,0"> <Grid HorizontalAlignment="Left"> <TextBlock Text="最大数量:" HorizontalAlignment="Left"/> - <TextBox x:Name="MaxThreads" Text="64" ToolTip="建议:2倍CPU核心数 + 1" HorizontalAlignment="Left" Margin="70,0,0,0" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> + <TextBox x:Name="MaxThreads" Text="64" ToolTip="默认:64,建议:2倍CPU核心数 + 1" HorizontalAlignment="Left" Margin="70,0,0,0" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> </Grid> </Grid> </StackPanel> </Grid> + <!-- 分割线 --> <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> - <!-- 结果列表每页显示条数 --> + <!-- 缓存池容量 --> <Grid> - <TextBlock Text="结果列表分页条数:" VerticalAlignment="Top" HorizontalAlignment="Left"/> + <TextBlock Text="缓存池容量:" VerticalAlignment="Top" HorizontalAlignment="Left"/> + <Grid Margin="20,30,20,0"> + <Grid HorizontalAlignment="Left"> + <TextBox x:Name="CachePoolCapacity" Text="100000" ToolTip="建议:根据自己CPU使用率和内存决定吧" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> + <TextBlock Text="默认:100000(缓存:文件被索引标记、文件内容等)" HorizontalAlignment="Left" Margin="110,0,0,0" /> + </Grid> + </Grid> + </Grid> + <!-- 分割线 --> + <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> + <!-- 结果列表分页条数 --> + <Grid> + <TextBlock Text="列表分页条数:" VerticalAlignment="Top" HorizontalAlignment="Left"/> <Grid Margin="20,30,20,0"> <Grid HorizontalAlignment="Left"> <TextBox x:Name="ResultListPageSize" Text="100" ToolTip="建议:大于50,小于300" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> - <TextBlock Text="(分页条数太多每页显示太多可能会卡顿)" HorizontalAlignment="Left" Margin="110,0,0,0" /> + <TextBlock Text="默认:100(每页显示太多可能会卡顿)" HorizontalAlignment="Left" Margin="110,0,0,0" /> </Grid> </Grid> </Grid> <!-- 分割线 --> <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> - <!-- 结果列表每页显示条数 --> + <!-- 文件读取超时时间 --> <Grid> <TextBlock Text="文件读取超时时间:" VerticalAlignment="Top" HorizontalAlignment="Left"/> <Grid Margin="20,30,20,0"> @@ -56,13 +69,25 @@ </Grid> <!-- 分割线 --> <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> - <!-- 结果列表每页显示条数 --> + <!-- 压缩包解析大小限制 --> <Grid> - <TextBlock Text="缓存池容量:" VerticalAlignment="Top" HorizontalAlignment="Left"/> + <TextBlock Text="压缩包解析大小限制:" VerticalAlignment="Top" HorizontalAlignment="Left"/> <Grid Margin="20,30,20,0"> <Grid HorizontalAlignment="Left"> - <TextBox x:Name="CachePoolCapacity" Text="100000" ToolTip="建议:根据自己CPU使用率和内存决定吧" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> - <TextBlock Text="程序运行中缓存(文件被索引标记、文件内容预览等)" HorizontalAlignment="Left" Margin="110,0,0,0" /> + <TextBox x:Name="ZipFileSizeLimit" Text="200" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> + <TextBlock Text="单位:MB(太大会导致索引创建时间变长或程序崩溃)" HorizontalAlignment="Left" Margin="110,0,0,0" /> + </Grid> + </Grid> + </Grid> + <!-- 分割线 --> + <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> + <!-- 是否解析压缩包内容 --> + <Grid> + <TextBlock Text="是否解析压缩包内容:" VerticalAlignment="Top" HorizontalAlignment="Left"/> + <Grid Margin="20,30,20,0"> + <Grid HorizontalAlignment="Left"> + <CheckBox x:Name="IsParseZipContent" Content="是否解析压缩包内容" HorizontalAlignment="Left" Width="145" input:InputMethod.IsInputMethodEnabled="False"/> + <TextBlock Text="默认:不启用(启用会导致创建索引时间变长)" HorizontalAlignment="Left" Margin="150,0,0,0" /> </Grid> </Grid> </Grid> diff --git a/TextLocator/SettingWindow.xaml.cs b/TextLocator/SettingWindow.xaml.cs index 947127b..336f547 100644 --- a/TextLocator/SettingWindow.xaml.cs +++ b/TextLocator/SettingWindow.xaml.cs @@ -68,6 +68,12 @@ namespace TextLocator // 缓存池容量 this.CachePoolCapacity.Text = AppConst.CACHE_POOL_CAPACITY + ""; + + // 压缩包解析限制大小 + this.ZipFileSizeLimit.Text = AppConst.ZIP_FILE_SIZE_LIMIT / 1000 / 1000 + ""; + + // 是否解析压缩包内容 + this.IsParseZipContent.IsChecked = AppConst.IS_PARSE_ZIP_CONTENT; } #region 保存并关闭 @@ -81,17 +87,6 @@ namespace TextLocator // 线程池 string minThreadsText = this.MinThreads.Text; string maxThreadsText = this.MaxThreads.Text; - - // 每页显示条数 - string ResultListPageSizeText = this.ResultListPageSize.Text; - - // 文件读取超时时间 - string fileReadTimeoutText = this.FileReadTimeout.Text; - - // 缓存池容量 - string cachePoolCapacityText = this.CachePoolCapacity.Text; - - // 转换,验证 int minThreads = 0; try { @@ -126,6 +121,26 @@ namespace TextLocator } } + // 缓存池容量 + string cachePoolCapacityText = this.CachePoolCapacity.Text; + int cachePoolCapacity = 0; + try + { + cachePoolCapacity = int.Parse(cachePoolCapacityText); + } + catch + { + Message.ShowWarning("MessageContainer", "缓存池容量设置错误"); + return; + } + if (cachePoolCapacity < 50000 || cachePoolCapacity > 500000) + { + Message.ShowWarning("MessageContainer", "建议设置在5-50W范围内"); + return; + } + + // 每页显示条数 + string ResultListPageSizeText = this.ResultListPageSize.Text; int ResultListPageSize = 0; try { @@ -142,6 +157,8 @@ namespace TextLocator return; } + // 文件读取超时时间 + string fileReadTimeoutText = this.FileReadTimeout.Text; int fileReadTimeout = 0; try { @@ -158,38 +175,50 @@ namespace TextLocator return; } - int cachePoolCapacity = 0; + // 压缩包解析大小限制 + string zipFileSizeLimitText = this.ZipFileSizeLimit.Text; + int zipFileSizeLimit = 0; try { - cachePoolCapacity = int.Parse(cachePoolCapacityText); - } catch + zipFileSizeLimit = int.Parse(zipFileSizeLimitText); + } + catch { - Message.ShowWarning("MessageContainer", "缓存池容量设置错误"); + Message.ShowWarning("MessageContainer", "压缩包解析大小限制错误"); return; } - if (cachePoolCapacity < 50000 || cachePoolCapacity > 500000) + if (zipFileSizeLimit > 200) { - Message.ShowWarning("MessageContainer", "建议设置在5-50w范围内"); + Message.ShowWarning("MessageContainer", "建议设置200MB范围内"); return; } + zipFileSizeLimit = zipFileSizeLimit * 1000 * 1000; + // 刷新、保存 AppConst.THREAD_POOL_MIN_SIZE = minThreads; AppConst.THREAD_POOL_MAX_SIZE = maxThreads; AppCore.SetThreadPoolSize(); + AppConst.CACHE_POOL_CAPACITY = cachePoolCapacity; + AppUtil.WriteValue("AppConfig", "CachePoolCapacity", AppConst.CACHE_POOL_CAPACITY + ""); + log.Debug("修改缓存池容量:" + AppConst.CACHE_POOL_CAPACITY); + AppConst.MRESULT_LIST_PAGE_SIZE = ResultListPageSize; AppUtil.WriteValue("AppConfig", "ResultListPageSize", AppConst.MRESULT_LIST_PAGE_SIZE + ""); log.Debug("修改结果列表分页条数:" + AppConst.MRESULT_LIST_PAGE_SIZE); - AppConst.FILE_READ_TIMEOUT = fileReadTimeout; AppUtil.WriteValue("AppConfig", "FileReadTimeout", AppConst.FILE_READ_TIMEOUT + ""); log.Debug("修改文件读取超时时间:" + AppConst.FILE_READ_TIMEOUT); - AppConst.CACHE_POOL_CAPACITY = cachePoolCapacity; - AppUtil.WriteValue("AppConfig", "CachePoolCapacity", AppConst.CACHE_POOL_CAPACITY + ""); - log.Debug("修改缓存池容量:" + AppConst.CACHE_POOL_CAPACITY); + AppConst.ZIP_FILE_SIZE_LIMIT = zipFileSizeLimit; + AppUtil.WriteValue("AppConfig", "ZipFileSizeLimit", AppConst.ZIP_FILE_SIZE_LIMIT + ""); + log.Debug("压缩包解析大小限制:" + AppConst.ZIP_FILE_SIZE_LIMIT); + + AppConst.IS_PARSE_ZIP_CONTENT = (bool)this.IsParseZipContent.IsChecked; + AppUtil.WriteValue("AppConfig", "IsParseZipContent", AppConst.IS_PARSE_ZIP_CONTENT + ""); + log.Debug("是否解析压缩包内容:" + AppConst.IS_PARSE_ZIP_CONTENT); this.Close(); } diff --git a/TextLocator/Util/RichTextBoxUtil.cs b/TextLocator/Util/RichTextBoxUtil.cs index aa4fa9b..8753a42 100644 --- a/TextLocator/Util/RichTextBoxUtil.cs +++ b/TextLocator/Util/RichTextBoxUtil.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; @@ -46,18 +49,18 @@ namespace TextLocator.Util TextPointer position = richTextBox.Document.ContentStart; while (position != null) { - //向前搜索,需要内容为Text + // 向前搜索,需要内容为Text if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text) { - //拿出Run的Text + // 拿出Run的Text string text = position.GetTextInRun(LogicalDirection.Forward); - //可能包含多个keyword,做遍历查找 - int index = text.IndexOf(keyword, 0); + // 可能包含多个keyword,做遍历查找 + int index = text.IndexOf(keyword, 0, StringComparison.CurrentCultureIgnoreCase); if (index != -1) { TextPointer start = position.GetPositionAtOffset(index); TextPointer end = start.GetPositionAtOffset(keyword.Length); - position = selecta(richTextBox, color, start, end, background); + position = Selecta(richTextBox, color, start, end, background); } } // 文字指针向前偏移 @@ -72,11 +75,10 @@ namespace TextLocator.Util /// </summary> /// <param name="richTextBox">元素</param> /// <param name="color">颜色</param> - /// <param name="selectLen">选择长度</param> /// <param name="tpStart">内容指针开始位置</param> /// <param name="tpEnd">内容指针结束位置</param> /// <returns></returns> - private static TextPointer selecta(RichTextBox richTextBox, Color color, TextPointer tpStart, TextPointer tpEnd, bool background) + private static TextPointer Selecta(RichTextBox richTextBox, Color color, TextPointer tpStart, TextPointer tpEnd, bool background) { TextRange range = richTextBox.Selection; range.Select(tpStart, tpEnd); -- Gitee From 3f5b3be19de76018bd257174f707e5a82c4324da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 00:11:19 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=89=88=E6=9C=AC=E6=9B=B4=E6=96=B01.2.12.?= =?UTF-8?q?0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Properties/AssemblyInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TextLocator/Properties/AssemblyInfo.cs b/TextLocator/Properties/AssemblyInfo.cs index 666cd99..90a184a 100644 --- a/TextLocator/Properties/AssemblyInfo.cs +++ b/TextLocator/Properties/AssemblyInfo.cs @@ -49,8 +49,8 @@ using System.Windows; //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 //通过使用 "*",如下所示: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.2.11")] -[assembly: AssemblyFileVersion("1.2.11.0")] +[assembly: AssemblyVersion("1.2.12")] +[assembly: AssemblyFileVersion("1.2.12.0")] // log4net [assembly: log4net.Config.XmlConfigurator(Watch = true)] \ No newline at end of file -- Gitee From 1d166051e309419b16b7bd803053f4dfe544fbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 00:14:09 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=8F=90=E7=A4=BA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/SettingWindow.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index c642dae..d124fae 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -39,7 +39,7 @@ <Grid Margin="20,30,20,0"> <Grid HorizontalAlignment="Left"> <TextBox x:Name="CachePoolCapacity" Text="100000" ToolTip="建议:根据自己CPU使用率和内存决定吧" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> - <TextBlock Text="默认:100000(缓存:文件被索引标记、文件内容等)" HorizontalAlignment="Left" Margin="110,0,0,0" /> + <TextBlock Text="默认:100000(缓存:被索引标记、文件内容等)" HorizontalAlignment="Left" Margin="110,0,0,0" /> </Grid> </Grid> </Grid> -- Gitee From d63742de6d7a4d2f63bd6ed96cf466a12b8c654d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 00:55:31 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85=E5=86=85=E5=AE=B9=E8=A7=A3=E6=9E=90=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=E5=88=A0=E9=99=A4=E9=80=BB=E8=BE=91=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E9=87=8A=E6=94=BE=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/Service/DevelopFileService.cs | 11 +++-- TextLocator/Service/DomFileService.cs | 11 +++-- TextLocator/Service/ExcelFileService.cs | 47 +++++++++++--------- TextLocator/Service/NoTextFileService.cs | 2 +- TextLocator/Service/PdfFileService.cs | 23 +++++----- TextLocator/Service/PowerPointFileService.cs | 37 ++++++++------- TextLocator/Service/TxtFileService.cs | 11 +++-- TextLocator/Service/WordFileService.cs | 27 ++++++----- TextLocator/Service/ZipFileService.cs | 7 ++- TextLocator/Util/FileUtil.cs | 8 ++-- 10 files changed, 105 insertions(+), 79 deletions(-) diff --git a/TextLocator/Service/DevelopFileService.cs b/TextLocator/Service/DevelopFileService.cs index f8ab5ff..eb57d5a 100644 --- a/TextLocator/Service/DevelopFileService.cs +++ b/TextLocator/Service/DevelopFileService.cs @@ -22,12 +22,15 @@ namespace TextLocator.Service StringBuilder builder = new StringBuilder(); try { - using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Encoding.UTF8)) + using (FileStream fs = File.OpenRead(filePath)) { - string line; - while ((line = reader.ReadLine()) != null) + using (StreamReader reader = new StreamReader(fs, Encoding.UTF8)) { - builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); + string line; + while ((line = reader.ReadLine()) != null) + { + builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); + } } } } diff --git a/TextLocator/Service/DomFileService.cs b/TextLocator/Service/DomFileService.cs index efc0a08..350c00d 100644 --- a/TextLocator/Service/DomFileService.cs +++ b/TextLocator/Service/DomFileService.cs @@ -22,12 +22,15 @@ namespace TextLocator.Service StringBuilder builder = new StringBuilder(); try { - using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Encoding.UTF8)) + using (FileStream fs = File.OpenRead(filePath)) { - string line; - while((line = reader.ReadLine()) != null) + using (StreamReader reader = new StreamReader(fs, Encoding.UTF8)) { - builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); + string line; + while ((line = reader.ReadLine()) != null) + { + builder.AppendLine(AppConst.REGEX_TAG.Replace(line, "")); + } } } } diff --git a/TextLocator/Service/ExcelFileService.cs b/TextLocator/Service/ExcelFileService.cs index d2e1d06..ac333ff 100644 --- a/TextLocator/Service/ExcelFileService.cs +++ b/TextLocator/Service/ExcelFileService.cs @@ -28,7 +28,7 @@ namespace TextLocator.Service { // =========== NPIO =========== // 文件流 - using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + using (FileStream fs = File.OpenRead(filePath)) { // 获取扩展名 string extName = Path.GetExtension(filePath); @@ -38,11 +38,11 @@ namespace TextLocator.Service { // 把xls写入workbook中 2003版本 case ".xls": - readWorkbook = new HSSFWorkbook(fileStream); + readWorkbook = new HSSFWorkbook(fs); break; // 把xlsx 写入workbook中 2007版本 case ".xlsx": - readWorkbook = new XSSFWorkbook(fileStream); + readWorkbook = new XSSFWorkbook(fs); break; default: break; @@ -97,40 +97,43 @@ namespace TextLocator.Service try { // =========== Spire.XLS =========== - // 创建Workbook对象 - using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook()) + using (FileStream fs = File.OpenRead(filePath)) { - // 加载Excel文档 - workbook.LoadFromFile(filePath); - StringBuilder builder = new StringBuilder(); - if (workbook != null) + // 创建Workbook对象 + using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook()) { - WorksheetsCollection sheets = workbook.Worksheets; - if (sheets != null && sheets.Count > 0) + // 加载Excel文档 + workbook.LoadFromStream(fs); + StringBuilder builder = new StringBuilder(); + if (workbook != null) { - // 获取工作表 - for (int i = 0; i < sheets.Count; i++) + WorksheetsCollection sheets = workbook.Worksheets; + if (sheets != null && sheets.Count > 0) { - using (Spire.Xls.Worksheet sheet = sheets[i]) + // 获取工作表 + for (int i = 0; i < sheets.Count; i++) { - // 行 - for (int j = sheet.FirstRow; j < sheet.LastRow; j++) + using (Spire.Xls.Worksheet sheet = sheets[i]) { - using (Spire.Xls.CellRange row = sheet.Rows[j]) + // 行 + for (int j = sheet.FirstRow; j < sheet.LastRow; j++) { - // 列 - for (int k = 0; k < row.Columns.Length; k++) + using (Spire.Xls.CellRange row = sheet.Rows[j]) { - builder.Append(row.Columns[k].Value2.ToString() + " "); + // 列 + for (int k = 0; k < row.Columns.Length; k++) + { + builder.Append(row.Columns[k].Value2.ToString() + " "); + } } + builder.AppendLine(); } - builder.AppendLine(); } } } } + content = builder.ToString(); } - content = builder.ToString(); } } catch (Exception ex) diff --git a/TextLocator/Service/NoTextFileService.cs b/TextLocator/Service/NoTextFileService.cs index c4deb20..59990a0 100644 --- a/TextLocator/Service/NoTextFileService.cs +++ b/TextLocator/Service/NoTextFileService.cs @@ -17,7 +17,7 @@ namespace TextLocator.Service try { System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath); - string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1); + string fileName = fileInfo.Name; StringBuilder builder = new StringBuilder(); builder.Append("文件名称:" + fileName); builder.Append(";更新时间:" + fileInfo.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")); diff --git a/TextLocator/Service/PdfFileService.cs b/TextLocator/Service/PdfFileService.cs index 3d3434d..0efb24a 100644 --- a/TextLocator/Service/PdfFileService.cs +++ b/TextLocator/Service/PdfFileService.cs @@ -24,20 +24,23 @@ namespace TextLocator.Service { try { - - // 实例化一个PdfDocument对象 - using (PdfDocument doc = new PdfDocument(new FileStream(filePath, FileMode.Open, FileAccess.Read))) + using (FileStream fs = File.OpenRead(filePath)) { - PdfPageCollection pages = doc.Pages; - if (pages != null && pages.Count > 0) + // 实例化一个PdfDocument对象 + using (PdfDocument doc = new PdfDocument(fs)) { - //提取PDF所有页面的文本 - foreach (PdfPageBase page in pages) + PdfPageCollection pages = doc.Pages; + if (pages != null && pages.Count > 0) { - try + //提取PDF所有页面的文本 + foreach (PdfPageBase page in pages) { - builder.Append(page.ExtractText().Replace("Evaluation Warning : The document was created with Spire.PDF for .NET.", "")); - } catch { } + try + { + builder.Append(page.ExtractText().Replace("Evaluation Warning : The document was created with Spire.PDF for .NET.", "")); + } + catch { } + } } } } diff --git a/TextLocator/Service/PowerPointFileService.cs b/TextLocator/Service/PowerPointFileService.cs index d51408f..0df7d63 100644 --- a/TextLocator/Service/PowerPointFileService.cs +++ b/TextLocator/Service/PowerPointFileService.cs @@ -24,40 +24,43 @@ namespace TextLocator.Service { try { - using (Presentation presentation = new Presentation(new FileStream(filePath, FileMode.Open, FileAccess.Read), FileFormat.Auto)) + using (FileStream fs = File.OpenRead(filePath)) { - SlideCollection slides = presentation.Slides; - if (slides != null && slides.Count > 0) + using (Presentation presentation = new Presentation(fs, FileFormat.Auto)) { - foreach (ISlide slide in presentation.Slides) + SlideCollection slides = presentation.Slides; + if (slides != null && slides.Count > 0) { - ShapeCollection shapes = slide.Shapes; - if (shapes != null && shapes.Count > 0) + foreach (ISlide slide in presentation.Slides) { - foreach (IShape shape in shapes) + ShapeCollection shapes = slide.Shapes; + if (shapes != null && shapes.Count > 0) { - if (shape != null && shape is IAutoShape) + foreach (IShape shape in shapes) { - ITextFrameProperties textFrame; - if ((textFrame = (shape as IAutoShape).TextFrame) != null) + if (shape != null && shape is IAutoShape) { - ParagraphCollection paragraph = textFrame.Paragraphs; - if (paragraph != null && paragraph.Count > 0) + ITextFrameProperties textFrame; + if ((textFrame = (shape as IAutoShape).TextFrame) != null) { - foreach (TextParagraph tp in paragraph) + ParagraphCollection paragraph = textFrame.Paragraphs; + if (paragraph != null && paragraph.Count > 0) { - builder.Append(tp.Text + Environment.NewLine); + foreach (TextParagraph tp in paragraph) + { + builder.Append(tp.Text + Environment.NewLine); + } } } } + shape.Dispose(); } - shape.Dispose(); } + slide.Dispose(); } - slide.Dispose(); } + } - } } catch (Exception ex) diff --git a/TextLocator/Service/TxtFileService.cs b/TextLocator/Service/TxtFileService.cs index 876955f..8bce107 100644 --- a/TextLocator/Service/TxtFileService.cs +++ b/TextLocator/Service/TxtFileService.cs @@ -20,12 +20,15 @@ namespace TextLocator.Service StringBuilder builder = new StringBuilder(); try { - using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read), Encoding.UTF8)) + using (FileStream fs = File.OpenRead(filePath)) { - string line; - while((line = reader.ReadLine()) != null) + using (StreamReader reader = new StreamReader(fs, Encoding.UTF8)) { - builder.AppendLine(line); + string line; + while ((line = reader.ReadLine()) != null) + { + builder.AppendLine(line); + } } } } diff --git a/TextLocator/Service/WordFileService.cs b/TextLocator/Service/WordFileService.cs index d355ad4..461660a 100644 --- a/TextLocator/Service/WordFileService.cs +++ b/TextLocator/Service/WordFileService.cs @@ -28,22 +28,25 @@ namespace TextLocator.Service try { // =========== Spire.XLS =========== - using (Spire.Doc.Document document = new Spire.Doc.Document(new FileStream(filePath, FileMode.Open, FileAccess.Read))) + using (FileStream fs = File.OpenRead(filePath)) { - if (document != null) + using (Spire.Doc.Document document = new Spire.Doc.Document(fs)) { - SectionCollection sections = document.Sections; - if (sections != null && sections.Count > 0) + if (document != null) { - // 提取每个段落的文本 - foreach (Section section in sections) + SectionCollection sections = document.Sections; + if (sections != null && sections.Count > 0) { - ParagraphCollection paragraphs = section.Paragraphs; - if (paragraphs != null && paragraphs.Count > 0) + // 提取每个段落的文本 + foreach (Section section in sections) { - foreach (Paragraph paragraph in paragraphs) + ParagraphCollection paragraphs = section.Paragraphs; + if (paragraphs != null && paragraphs.Count > 0) { - builder.AppendLine(paragraph.Text); + foreach (Paragraph paragraph in paragraphs) + { + builder.AppendLine(paragraph.Text); + } } } } @@ -57,9 +60,9 @@ namespace TextLocator.Service XWPFDocument document = null; try { - using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + using (FileStream fs = File.OpenRead(filePath)) { - document = new XWPFDocument(file); + document = new XWPFDocument(fs); if (document != null) { diff --git a/TextLocator/Service/ZipFileService.cs b/TextLocator/Service/ZipFileService.cs index 3fbf2f0..e5124c1 100644 --- a/TextLocator/Service/ZipFileService.cs +++ b/TextLocator/Service/ZipFileService.cs @@ -29,7 +29,7 @@ namespace TextLocator.Service // 内容 StringBuilder builder = new StringBuilder(); lock (locker) - { + { try { // 文件信息 @@ -64,6 +64,9 @@ namespace TextLocator.Service } builder.Append(GetContent(filePath, unzipPath)); + + // 清理临时目录 + FileUtil.RemoveDirectory(unzipPath); } else { @@ -88,7 +91,7 @@ namespace TextLocator.Service private string GetContent(string filePath, string unzipPath) { StringBuilder builder = new StringBuilder(); - using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) + using (FileStream file = File.OpenRead(filePath)) { using (var archive = ArchiveFactory.Open(file)) { diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 735106e..c045913 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -209,13 +209,15 @@ namespace TextLocator.Util } catch (Exception ex) { - log.Error("文件删除失败:" + ex.Message, ex); + log.Error(fileInfo.FullName + " -> 文件删除失败:" + ex.Message, ex); } } try { - File.Delete(srcDir); - } catch { } + Directory.Delete(srcDir); + } catch (Exception ex) { + log.Error(srcDir + " -> 目录删除失败:" + ex.Message, ex); + } } /// <summary> -- Gitee From 677e94f7d538b923b9cca5d407b8556fa1662370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 11:16:54 +0800 Subject: [PATCH 5/6] =?UTF-8?q?zip=E8=A7=A3=E6=9E=90=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 14 +++++++++++++- TextLocator/Service/ZipFileService.cs | 1 - TextLocator/Util/FileUtil.cs | 16 ++-------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index aeec441..ff4e0a0 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -119,8 +119,20 @@ namespace TextLocator /// </summary> private void InitAppConfig() { - // 保存文件读取超时时间 + // 保存缓存池容量 + AppUtil.WriteValue("AppConfig", "CachePoolCapacity", AppConst.CACHE_POOL_CAPACITY + ""); + + // 每页显示条数 + AppUtil.WriteValue("AppConfig", "ResultListPageSize", AppConst.MRESULT_LIST_PAGE_SIZE + ""); + + // 文件读取超时时间 AppUtil.WriteValue("AppConfig", "FileReadTimeout", AppConst.FILE_READ_TIMEOUT + ""); + + // 压缩包解析大小限制 + AppUtil.WriteValue("AppConfig", "ZipFileSizeLimit", AppConst.ZIP_FILE_SIZE_LIMIT + ""); + + // 是否解析压缩包内容 + AppUtil.WriteValue("AppConfig", "IsParseZipContent", AppConst.IS_PARSE_ZIP_CONTENT + ""); } #endregion diff --git a/TextLocator/Service/ZipFileService.cs b/TextLocator/Service/ZipFileService.cs index e5124c1..b64efcb 100644 --- a/TextLocator/Service/ZipFileService.cs +++ b/TextLocator/Service/ZipFileService.cs @@ -86,7 +86,6 @@ namespace TextLocator.Service /// </summary> /// <param name="filePath">压缩文件路径</param> /// <param name="unzipPath">解压路径</param> - /// <param name="isParseContent">是否解析文件内容</param> /// <returns></returns> private string GetContent(string filePath, string unzipPath) { diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index c045913..bf1bc9c 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -199,22 +199,10 @@ namespace TextLocator.Util { return; } - // 获取源文件夹中的所有文件完整路径 - FileInfo[] files = new DirectoryInfo(srcDir).GetFiles(); - foreach (FileInfo fileInfo in files) - { - try - { - File.Delete(fileInfo.FullName); - } - catch (Exception ex) - { - log.Error(fileInfo.FullName + " -> 文件删除失败:" + ex.Message, ex); - } - } try { - Directory.Delete(srcDir); + DirectoryInfo dirInfo = new DirectoryInfo(srcDir); + dirInfo.Delete(true); } catch (Exception ex) { log.Error(srcDir + " -> 目录删除失败:" + ex.Message, ex); } -- Gitee From 21a9601c2ae99b8d182a3edf457e237875482f02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A3=8A?= <liulei901112@163.com> Date: Wed, 30 Mar 2022 20:57:53 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=8E=8B=E7=BC=A9?= =?UTF-8?q?=E5=8C=85=E5=86=85=E5=AE=B9=E8=A7=A3=E6=9E=90=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=8F=AA=E8=A6=81=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=86=85=E5=AE=B9=20=E7=B4=A2=E5=BC=95=E5=B0=B1OOM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TextLocator/App.xaml.cs | 6 -- TextLocator/Core/AppConst.cs | 10 +--- TextLocator/Index/IndexCore.cs | 19 +++---- TextLocator/Service/ZipFileService.cs | 82 +++------------------------ TextLocator/SettingWindow.xaml | 24 -------- TextLocator/SettingWindow.xaml.cs | 34 ----------- 6 files changed, 18 insertions(+), 157 deletions(-) diff --git a/TextLocator/App.xaml.cs b/TextLocator/App.xaml.cs index ff4e0a0..75750fd 100644 --- a/TextLocator/App.xaml.cs +++ b/TextLocator/App.xaml.cs @@ -127,12 +127,6 @@ namespace TextLocator // 文件读取超时时间 AppUtil.WriteValue("AppConfig", "FileReadTimeout", AppConst.FILE_READ_TIMEOUT + ""); - - // 压缩包解析大小限制 - AppUtil.WriteValue("AppConfig", "ZipFileSizeLimit", AppConst.ZIP_FILE_SIZE_LIMIT + ""); - - // 是否解析压缩包内容 - AppUtil.WriteValue("AppConfig", "IsParseZipContent", AppConst.IS_PARSE_ZIP_CONTENT + ""); } #endregion diff --git a/TextLocator/Core/AppConst.cs b/TextLocator/Core/AppConst.cs index a56bb1a..ad8d23a 100644 --- a/TextLocator/Core/AppConst.cs +++ b/TextLocator/Core/AppConst.cs @@ -31,15 +31,7 @@ namespace TextLocator.Core /// <summary> /// 文件大小限制 /// </summary> - public static int FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "FileSizeLimit", "500000000")); - /// <summary> - /// 压缩包解析大小 - /// </summary> - public static int ZIP_FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "ZipFileSizeLimit", "100000000")); - /// <summary> - /// 是否解析压缩包内容 - /// </summary> - public static bool IS_PARSE_ZIP_CONTENT = bool.Parse(AppUtil.ReadValue("AppConfig", "IsParseZipContent", "false")); + public static int FILE_SIZE_LIMIT = int.Parse(AppUtil.ReadValue("AppConfig", "FileSizeLimit", "200000000")); /// <summary> /// 缓存池容量 /// </summary> diff --git a/TextLocator/Index/IndexCore.cs b/TextLocator/Index/IndexCore.cs index d5c5a24..996586a 100644 --- a/TextLocator/Index/IndexCore.cs +++ b/TextLocator/Index/IndexCore.cs @@ -41,10 +41,6 @@ namespace TextLocator.Index /// </summary> private static volatile int _totalCount = 0; /// <summary> - /// 是否是创建 - /// </summary> - private static volatile bool _create = false; - /// <summary> /// 回调函数 /// </summary> private static volatile Callback _callback; @@ -58,7 +54,8 @@ namespace TextLocator.Index /// <summary> /// 创建索引写入器 /// </summary> - private static void CreateIndexWriter() + /// <param name="create">是否是创建</param> + private static void CreateIndexWriter(bool create = false) { if (_indexWriter == null) { @@ -69,7 +66,7 @@ namespace TextLocator.Index // 分词器 AppConst.INDEX_ANALYZER, // 是否创建 - _create, + create, // 字段限制 IndexWriter.MaxFieldLength.UNLIMITED); @@ -114,22 +111,22 @@ namespace TextLocator.Index _finishCount = 0; // 判断是创建索引还是增量索引(如果索引目录不存在,重建) - _create = !Directory.Exists(AppConst.APP_INDEX_DIR); + bool create = !Directory.Exists(AppConst.APP_INDEX_DIR); // 入参为true,表示重建 if (rebuild) { - _create = rebuild; + create = rebuild; } // 创建还是更新? - if (_create) + if (create) { // 重建时,删除全部已建索引的标记 AppUtil.DeleteSection("FileIndex"); } // 创建索引写入器 - CreateIndexWriter(); + CreateIndexWriter(create); using (MutipleThreadResetEvent resetEvent = new MutipleThreadResetEvent(_totalCount)) { @@ -138,7 +135,7 @@ namespace TextLocator.Index { string filePath = filePaths[i]; // 忽略已存在索引的文件 - if (SkipFile(_create, filePath, resetEvent)) + if (SkipFile(create, filePath, resetEvent)) { continue; } diff --git a/TextLocator/Service/ZipFileService.cs b/TextLocator/Service/ZipFileService.cs index b64efcb..9d65931 100644 --- a/TextLocator/Service/ZipFileService.cs +++ b/TextLocator/Service/ZipFileService.cs @@ -44,33 +44,20 @@ namespace TextLocator.Service builder.Append(" 大小:" + FileUtil.GetFileSizeFriendly(fileInfo.Length)); builder.Append(" 列表:=> \r\n"); - // 解析文件内容 && 文件大小 - if (AppConst.IS_PARSE_ZIP_CONTENT && fileSize <= AppConst.ZIP_FILE_SIZE_LIMIT) + // 解析列表 + using (FileStream file = File.OpenRead(filePath)) { - // 获取文件信息,判断压缩包大小。大于限制大小的同样不解析文件内容 - string unzipPath = Path.Combine(AppConst.APP_TEMP_DIR, fileName); - - // 判断文件夹是否存在,不存在创建 - if (!Directory.Exists(unzipPath)) + using (var archive = ArchiveFactory.Open(file)) { - try - { - Directory.CreateDirectory(unzipPath); - } - catch (Exception ex) + foreach (var entry in archive.Entries) { - log.Error("解压文件夹创建失败:" + ex.Message, ex); + if (!entry.IsDirectory) + { + builder.Append("┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\r\n"); + builder.Append(string.Format("{0}, {1}\r\n", entry.Key, FileUtil.GetFileSizeFriendly(entry.Size))); + } } } - - builder.Append(GetContent(filePath, unzipPath)); - - // 清理临时目录 - FileUtil.RemoveDirectory(unzipPath); - } - else - { - builder.Append(GetContent(filePath, null)); } } catch (Exception ex) @@ -80,56 +67,5 @@ namespace TextLocator.Service } return builder.ToString(); } - - /// <summary> - /// 获取内容 - /// </summary> - /// <param name="filePath">压缩文件路径</param> - /// <param name="unzipPath">解压路径</param> - /// <returns></returns> - private string GetContent(string filePath, string unzipPath) - { - StringBuilder builder = new StringBuilder(); - using (FileStream file = File.OpenRead(filePath)) - { - using (var archive = ArchiveFactory.Open(file)) - { - foreach (var entry in archive.Entries) - { - if (!entry.IsDirectory) - { - builder.Append("┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄\r\n"); - builder.Append(string.Format("{0}, {1}\r\n", entry.Key, FileUtil.GetFileSizeFriendly(entry.Size))); - // 解压路径不为空,代表需要解压并解析 - if (!string.IsNullOrEmpty(unzipPath)) - { - // 获取文件类型 - FileType fileType = FileTypeUtil.GetFileType(entry.Key); - // 文件后缀 - string fileExt = Path.GetExtension(entry.Key); - if (!string.IsNullOrEmpty(fileExt)) - { - fileExt = fileExt.Substring(1); - } - // 支持的文件后缀集 - string fileExts = FileTypeUtil.GetFileTypeExts(); - // 不是压缩包 && 文件后缀在可解析范围 - if (fileType != FileType.压缩包 && fileExts.Contains(fileExt)) - { - // 判断文件是否支持解析,不支持解析的文件无需解压 - entry.WriteToDirectory(unzipPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); - - // 获取文件信息,判断压缩包大小。大于限制大小的同样不解析文件内容 - string unzipFile = Path.Combine(unzipPath, entry.Key); - // 解析文件内容 - builder.Append(FileInfoServiceFactory.GetFileContent(unzipFile) + "\r\n"); - } - } - } - } - } - } - return builder.ToString(); - } } } diff --git a/TextLocator/SettingWindow.xaml b/TextLocator/SettingWindow.xaml index d124fae..05b8fa0 100644 --- a/TextLocator/SettingWindow.xaml +++ b/TextLocator/SettingWindow.xaml @@ -67,30 +67,6 @@ </Grid> </Grid> </Grid> - <!-- 分割线 --> - <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> - <!-- 压缩包解析大小限制 --> - <Grid> - <TextBlock Text="压缩包解析大小限制:" VerticalAlignment="Top" HorizontalAlignment="Left"/> - <Grid Margin="20,30,20,0"> - <Grid HorizontalAlignment="Left"> - <TextBox x:Name="ZipFileSizeLimit" Text="200" HorizontalAlignment="Left" Width="100" input:InputMethod.IsInputMethodEnabled="False" PreviewTextInput="Number_TextBox_PreviewTextInput"/> - <TextBlock Text="单位:MB(太大会导致索引创建时间变长或程序崩溃)" HorizontalAlignment="Left" Margin="110,0,0,0" /> - </Grid> - </Grid> - </Grid> - <!-- 分割线 --> - <Line X1="0" Y1="0" X2="480" Y2="0" Margin="0,10,0,10" Stroke="Gray" StrokeThickness="1"></Line> - <!-- 是否解析压缩包内容 --> - <Grid> - <TextBlock Text="是否解析压缩包内容:" VerticalAlignment="Top" HorizontalAlignment="Left"/> - <Grid Margin="20,30,20,0"> - <Grid HorizontalAlignment="Left"> - <CheckBox x:Name="IsParseZipContent" Content="是否解析压缩包内容" HorizontalAlignment="Left" Width="145" input:InputMethod.IsInputMethodEnabled="False"/> - <TextBlock Text="默认:不启用(启用会导致创建索引时间变长)" HorizontalAlignment="Left" Margin="150,0,0,0" /> - </Grid> - </Grid> - </Grid> </StackPanel> </ScrollViewer> </Grid> diff --git a/TextLocator/SettingWindow.xaml.cs b/TextLocator/SettingWindow.xaml.cs index 336f547..4321b69 100644 --- a/TextLocator/SettingWindow.xaml.cs +++ b/TextLocator/SettingWindow.xaml.cs @@ -68,12 +68,6 @@ namespace TextLocator // 缓存池容量 this.CachePoolCapacity.Text = AppConst.CACHE_POOL_CAPACITY + ""; - - // 压缩包解析限制大小 - this.ZipFileSizeLimit.Text = AppConst.ZIP_FILE_SIZE_LIMIT / 1000 / 1000 + ""; - - // 是否解析压缩包内容 - this.IsParseZipContent.IsChecked = AppConst.IS_PARSE_ZIP_CONTENT; } #region 保存并关闭 @@ -175,26 +169,6 @@ namespace TextLocator return; } - // 压缩包解析大小限制 - string zipFileSizeLimitText = this.ZipFileSizeLimit.Text; - int zipFileSizeLimit = 0; - try - { - zipFileSizeLimit = int.Parse(zipFileSizeLimitText); - } - catch - { - Message.ShowWarning("MessageContainer", "压缩包解析大小限制错误"); - return; - } - if (zipFileSizeLimit > 200) - { - Message.ShowWarning("MessageContainer", "建议设置200MB范围内"); - return; - } - zipFileSizeLimit = zipFileSizeLimit * 1000 * 1000; - - // 刷新、保存 AppConst.THREAD_POOL_MIN_SIZE = minThreads; AppConst.THREAD_POOL_MAX_SIZE = maxThreads; @@ -212,14 +186,6 @@ namespace TextLocator AppUtil.WriteValue("AppConfig", "FileReadTimeout", AppConst.FILE_READ_TIMEOUT + ""); log.Debug("修改文件读取超时时间:" + AppConst.FILE_READ_TIMEOUT); - AppConst.ZIP_FILE_SIZE_LIMIT = zipFileSizeLimit; - AppUtil.WriteValue("AppConfig", "ZipFileSizeLimit", AppConst.ZIP_FILE_SIZE_LIMIT + ""); - log.Debug("压缩包解析大小限制:" + AppConst.ZIP_FILE_SIZE_LIMIT); - - AppConst.IS_PARSE_ZIP_CONTENT = (bool)this.IsParseZipContent.IsChecked; - AppUtil.WriteValue("AppConfig", "IsParseZipContent", AppConst.IS_PARSE_ZIP_CONTENT + ""); - log.Debug("是否解析压缩包内容:" + AppConst.IS_PARSE_ZIP_CONTENT); - this.Close(); } #endregion -- Gitee