diff --git a/TextLocator/Enums/FileType.cs b/TextLocator/Enums/FileType.cs index 371d0406bb878fd41afd3d505482a8535023ec4b..73ed1eb5ffcc62d3a05f04cd535a5f66c874e4b7 100644 --- a/TextLocator/Enums/FileType.cs +++ b/TextLocator/Enums/FileType.cs @@ -40,7 +40,7 @@ namespace TextLocator.Enums /// /// 代码文件 /// - [Description("css,js,java,cs,md")] + [Description("cs,java,js,css,md")] 代码文件, /// /// 纯文本 diff --git a/TextLocator/Index/LuceneIndexCore.cs b/TextLocator/Index/LuceneIndexCore.cs index 91c71e5aa0e5b297572410e4e95a0a5114ccc823..0cca55c5b7b13b964465409ab4c10ac37bfe9f44 100644 --- a/TextLocator/Index/LuceneIndexCore.cs +++ b/TextLocator/Index/LuceneIndexCore.cs @@ -139,20 +139,20 @@ namespace TextLocator.Index // 根据文件路径获取文件类型(自定义文件类型分类) FileType fileType = FileTypeUtil.GetFileType(filePath); - lock (locker) - { - IFileInfoService fileInfoService = FileInfoServiceFactory.GetFileInfoService(fileType); + IFileInfoService fileInfoService = FileInfoServiceFactory.GetFileInfoService(fileType); - // 文件内容 - string content = fileInfoService.GetFileContent(filePath); + // 文件内容 + string content = fileInfoService.GetFileContent(filePath); - // 缩略信息 - string breviary = new Regex(" |\r|\n|\\s").Replace(content, ""); - if (breviary.Length > 150) - { - breviary = breviary.Substring(0, 150) + "..."; - } + // 缩略信息 + string breviary = new Regex(" |\r|\n|\\s").Replace(content, ""); + if (breviary.Length > 150) + { + breviary = breviary.Substring(0, 150) + "..."; + } + lock (locker) + { // 当索引文件中含有与filemark相等的field值时,会先删除再添加,以防出现重复 indexWriter.DeleteDocuments(new Lucene.Net.Index.Term("FileMark", fileMark)); diff --git a/TextLocator/Properties/Resources.Designer.cs b/TextLocator/Properties/Resources.Designer.cs index e8b929fb0bb39b0fa9904977e3295c4c5ddc9547..bf5f86dcc3382dc6193d14a967ff32d3fd4a30e6 100644 --- a/TextLocator/Properties/Resources.Designer.cs +++ b/TextLocator/Properties/Resources.Designer.cs @@ -70,16 +70,6 @@ namespace TextLocator.Properties { } } - /// - /// 查找 System.Drawing.Bitmap 类型的本地化资源。 - /// - internal static System.Drawing.Bitmap eml { - get { - object obj = ResourceManager.GetObject("eml", resourceCulture); - return ((System.Drawing.Bitmap)(obj)); - } - } - /// /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// @@ -103,9 +93,9 @@ namespace TextLocator.Properties { /// /// 查找 System.Drawing.Bitmap 类型的本地化资源。 /// - internal static System.Drawing.Bitmap jpg { + internal static System.Drawing.Bitmap image { get { - object obj = ResourceManager.GetObject("jpg", resourceCulture); + object obj = ResourceManager.GetObject("image", resourceCulture); return ((System.Drawing.Bitmap)(obj)); } } diff --git a/TextLocator/Properties/Resources.resx b/TextLocator/Properties/Resources.resx index 04f4ae2243d463873983e8f3700266f606bdece7..54491892d3363721a359496324f24d2d41661772 100644 --- a/TextLocator/Properties/Resources.resx +++ b/TextLocator/Properties/Resources.resx @@ -121,17 +121,14 @@ ..\Resource\ext\code.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - - ..\Resource\ext\eml.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - ..\Resource\ext\excel.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Resource\ext\html.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a - - ..\Resource\ext\jpg.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + ..\Resource\ext\image.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ..\Resource\ext\pdf.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/TextLocator/Resource/ext/code.png b/TextLocator/Resource/ext/code.png index 6c78e441ad7330788c836905db9a1c3475f22da0..144a22c256ab1ab1ab511f187c5795246c583693 100644 Binary files a/TextLocator/Resource/ext/code.png and b/TextLocator/Resource/ext/code.png differ diff --git a/TextLocator/Resource/ext/eml.png b/TextLocator/Resource/ext/eml.png deleted file mode 100644 index c1e0580a12ebf4d35059eaa1f77c9aeee53d20ef..0000000000000000000000000000000000000000 Binary files a/TextLocator/Resource/ext/eml.png and /dev/null differ diff --git a/TextLocator/Resource/ext/excel.png b/TextLocator/Resource/ext/excel.png index 529d265bbf1866086ef41010fc31e8eb4187a4a9..82211fc400ad11d3cf70e7a91d606974f665739a 100644 Binary files a/TextLocator/Resource/ext/excel.png and b/TextLocator/Resource/ext/excel.png differ diff --git a/TextLocator/Resource/ext/image.png b/TextLocator/Resource/ext/image.png new file mode 100644 index 0000000000000000000000000000000000000000..30553b1c327503021058e42c076556c1b9fb9d66 Binary files /dev/null and b/TextLocator/Resource/ext/image.png differ diff --git a/TextLocator/Resource/ext/jpg.png b/TextLocator/Resource/ext/jpg.png deleted file mode 100644 index 65fde8f648a39aa66235c5515355baab96bc0a0f..0000000000000000000000000000000000000000 Binary files a/TextLocator/Resource/ext/jpg.png and /dev/null differ diff --git a/TextLocator/Resource/ext/pdf.png b/TextLocator/Resource/ext/pdf.png index 96f1c31df6641cbe2cadf5336d5703ccd29cc3ef..0845f6f16804964bbd02335f564f24072816eae1 100644 Binary files a/TextLocator/Resource/ext/pdf.png and b/TextLocator/Resource/ext/pdf.png differ diff --git a/TextLocator/Resource/ext/ppt.png b/TextLocator/Resource/ext/ppt.png index 6b58b55de833959ae05e6d79974433be16dfc12c..8237436b3be877b0266891d114585c12b4654ea6 100644 Binary files a/TextLocator/Resource/ext/ppt.png and b/TextLocator/Resource/ext/ppt.png differ diff --git a/TextLocator/Resource/ext/word.png b/TextLocator/Resource/ext/word.png index 73e2636af33def29a87959be39dfef2558aea5d7..47bee182b35c20561987dc09245873dc5c2f76d0 100644 Binary files a/TextLocator/Resource/ext/word.png and b/TextLocator/Resource/ext/word.png differ diff --git a/TextLocator/Service/DevelopFileService.cs b/TextLocator/Service/DevelopFileService.cs index 1ad23345e076d1a75075d2478f52a1a032e39232..a5c8d71da56d05075966c983509190dc42445008 100644 --- a/TextLocator/Service/DevelopFileService.cs +++ b/TextLocator/Service/DevelopFileService.cs @@ -16,24 +16,32 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static volatile object locker = new object(); + public string GetFileContent(string filePath) { // 获取扩展名 string extName = Path.GetExtension(filePath); // 文件内容 string content = ""; - try + lock (locker) { - using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8)) + try { - content = reader.ReadToEnd(); + using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8)) + { + content = reader.ReadToEnd(); + + content = Regex.Replace(content, "\\<.[^<>]*\\>", ""); - content = Regex.Replace(content, "\\<.[^<>]*\\>", ""); + reader.Close(); + reader.Dispose(); + } + } + catch (Exception ex) + { + log.Error(ex.Message, ex); } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); } return content; } diff --git a/TextLocator/Service/ExcelFileService.cs b/TextLocator/Service/ExcelFileService.cs index 9be7b54f0becb59f658e8b79109707ea6f599cba..ec653dd92e3957ea034bebb66f2130131500aa7c 100644 --- a/TextLocator/Service/ExcelFileService.cs +++ b/TextLocator/Service/ExcelFileService.cs @@ -15,119 +15,124 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static volatile object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { try { - // =========== NPIO =========== - - // 获取扩展名 - string extName = Path.GetExtension(filePath); - // 文件流 - using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) + try { - // 读取IWorkbook - IWorkbook readWorkbook = null; - switch (extName) - { - // 把xls写入workbook中 2003版本 - case ".xls": - readWorkbook = new HSSFWorkbook(fileStream); - break; - // 把xlsx 写入workbook中 2007版本 - case ".xlsx": - readWorkbook = new XSSFWorkbook(fileStream); - break; - default: - break; - } - // 关闭文件流 - fileStream.Close(); + // =========== NPIO =========== - if (readWorkbook != null) + // 获取扩展名 + string extName = Path.GetExtension(filePath); + // 文件流 + using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite)) { - StringBuilder builder = new StringBuilder(); - // 获取表 - var sheetCount = readWorkbook.NumberOfSheets; - for (int i = 0; i < sheetCount; i++) + // 读取IWorkbook + IWorkbook readWorkbook = null; + switch (extName) { - // 获取sheet表数据 - ISheet sheet = readWorkbook.GetSheetAt(i); - - // 获取行数 - var rowCount = sheet.LastRowNum; + // 把xls写入workbook中 2003版本 + case ".xls": + readWorkbook = new HSSFWorkbook(fileStream); + break; + // 把xlsx 写入workbook中 2007版本 + case ".xlsx": + readWorkbook = new XSSFWorkbook(fileStream); + break; + default: + break; + } + // 关闭文件流 + fileStream.Close(); - // 从第四行(下标为3)开始获取数据,前三行是表头 - // 如果从第一行开始,则i=0就可以了 - for (int j = 0; j <= rowCount; j++) + if (readWorkbook != null) + { + StringBuilder builder = new StringBuilder(); + // 获取表 + var sheetCount = readWorkbook.NumberOfSheets; + for (int i = 0; i < sheetCount; i++) { - // 获取具体行 - IRow row = sheet.GetRow(j); - if (row != null) + // 获取sheet表数据 + ISheet sheet = readWorkbook.GetSheetAt(i); + + // 获取行数 + var rowCount = sheet.LastRowNum; + + // 从第四行(下标为3)开始获取数据,前三行是表头 + // 如果从第一行开始,则i=0就可以了 + for (int j = 0; j <= rowCount; j++) { - // 获取行对应的列数 - for (int k = 0; k < row.LastCellNum; k++) + // 获取具体行 + IRow row = sheet.GetRow(j); + if (row != null) { - // 获取某行某列对应的单元格数据 - builder.Append(row.GetCell(k) + ";"); + // 获取行对应的列数 + for (int k = 0; k < row.LastCellNum; k++) + { + // 获取某行某列对应的单元格数据 + builder.Append(row.GetCell(k) + ";"); + } + // 换行 + builder.AppendLine(); } - // 换行 - builder.AppendLine(); } + + readWorkbook.Close(); } - - readWorkbook.Close(); - } - content = builder.ToString(); + content = builder.ToString(); + } } } - } - catch - { - // =========== Spire.XLS =========== - // 创建Workbook对象 - using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook()) + catch { + // =========== Spire.XLS =========== + // 创建Workbook对象 + using (Spire.Xls.Workbook workbook = new Spire.Xls.Workbook()) + { - // 加载Excel文档 - workbook.LoadFromFile(filePath); - - StringBuilder builder = new StringBuilder(); + // 加载Excel文档 + workbook.LoadFromFile(filePath); - // 获取工作表 - for (int i = 0; i < workbook.Worksheets.Count; i++) - { - Spire.Xls.Worksheet sheet = workbook.Worksheets[i]; + StringBuilder builder = new StringBuilder(); - // 行 - for (int j = sheet.FirstRow; j < sheet.LastRow; j++) + // 获取工作表 + for (int i = 0; i < workbook.Worksheets.Count; i++) { - Spire.Xls.CellRange row = sheet.Rows[j]; - // 列 - for (int k = 0; k < row.Columns.Length; k++) + Spire.Xls.Worksheet sheet = workbook.Worksheets[i]; + + // 行 + for (int j = sheet.FirstRow; j < sheet.LastRow; j++) { - builder.Append(row.Columns[k].Value2.ToString()); + Spire.Xls.CellRange row = sheet.Rows[j]; + // 列 + for (int k = 0; k < row.Columns.Length; k++) + { + builder.Append(row.Columns[k].Value2.ToString()); + } + row.Dispose(); + builder.AppendLine(); } - row.Dispose(); - builder.AppendLine(); + sheet.Dispose(); } - sheet.Dispose(); - } - workbook.Dispose(); + workbook.Dispose(); - content = builder.ToString(); + content = builder.ToString(); + } } } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); + catch (Exception ex) + { + log.Error(ex.Message, ex); + } } return content; } diff --git a/TextLocator/Service/PdfFileService.cs b/TextLocator/Service/PdfFileService.cs index d9cbd12ccabb214ec272e4478417e07ba3ea8739..d626c5b4a5c9b79cb18faeb0575ba04b8cbd0797 100644 --- a/TextLocator/Service/PdfFileService.cs +++ b/TextLocator/Service/PdfFileService.cs @@ -13,36 +13,41 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static volatile object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { - // 实例化一个PdfDocument对象 - using (PdfDocument doc = new PdfDocument()) + try { - //实例化一个StringBuilder 对象 - StringBuilder builder = new StringBuilder(); - // 加载Pdf文档 - doc.LoadFromFile(filePath); - - //提取PDF所有页面的文本 - foreach (PdfPageBase page in doc.Pages) + // 实例化一个PdfDocument对象 + using (PdfDocument doc = new PdfDocument()) { - builder.Append(page.ExtractText()); + //实例化一个StringBuilder 对象 + StringBuilder builder = new StringBuilder(); + // 加载Pdf文档 + doc.LoadFromFile(filePath); + + //提取PDF所有页面的文本 + foreach (PdfPageBase page in doc.Pages) + { + builder.Append(page.ExtractText()); + } + + doc.Dispose(); + + content = builder.ToString(); } - doc.Dispose(); - content = builder.ToString(); } - - - } - catch (Exception ex) - { - log.Error(ex.Message, ex); + catch (Exception ex) + { + log.Error(ex.Message, ex); + } } return content.Replace("Evaluation Warning : The document was created with Spire.PDF for .NET. ", ""); } diff --git a/TextLocator/Service/PowerPointFileService.cs b/TextLocator/Service/PowerPointFileService.cs index e6d7423766fe0653548c9f29889b4d16794bbfbe..2e351e8e5774cb483f19242a3db9da1279dd025b 100644 --- a/TextLocator/Service/PowerPointFileService.cs +++ b/TextLocator/Service/PowerPointFileService.cs @@ -12,38 +12,44 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static volatile object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { - using (Presentation presentation = new Presentation(filePath, FileFormat.Auto)) { - StringBuilder builder = new StringBuilder(); - foreach (ISlide slide in presentation.Slides) + try + { + using (Presentation presentation = new Presentation(filePath, FileFormat.Auto)) { - foreach (IShape shape in slide.Shapes) + StringBuilder builder = new StringBuilder(); + foreach (ISlide slide in presentation.Slides) { - if (shape is IAutoShape) + foreach (IShape shape in slide.Shapes) { - if ((shape as IAutoShape).TextFrame != null) + if (shape is IAutoShape) { - foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs) + if ((shape as IAutoShape).TextFrame != null) { - builder.Append(tp.Text + Environment.NewLine); + foreach (TextParagraph tp in (shape as IAutoShape).TextFrame.Paragraphs) + { + builder.Append(tp.Text + Environment.NewLine); + } } } + shape.Dispose(); } - shape.Dispose(); + slide.Dispose(); } - slide.Dispose(); + content = builder.ToString(); } - content = builder.ToString(); } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); + catch (Exception ex) + { + log.Error(ex.Message, ex); + } } return content; } diff --git a/TextLocator/Service/TxtFileService.cs b/TextLocator/Service/TxtFileService.cs index 5ecbbef1da335717cdd2bd2e4b3b8f2ef67122b6..063219adb3216ed508aa6d0ee66e698587e4ac84 100644 --- a/TextLocator/Service/TxtFileService.cs +++ b/TextLocator/Service/TxtFileService.cs @@ -12,27 +12,34 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static volatile object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { - using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)) + try { - StringBuilder builder = new StringBuilder(); - string line; - while ((line = reader.ReadLine()) != null) + using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8)) { - builder.Append(line); - } + StringBuilder builder = new StringBuilder(); + string line; + while ((line = reader.ReadLine()) != null) + { + builder.Append(line); + } + reader.Close(); + reader.Dispose(); - content = builder.ToString(); + content = builder.ToString(); + } + } + catch (Exception ex) + { + log.Error(ex.Message, ex); } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); } return content; } diff --git a/TextLocator/Service/WordFileService.cs b/TextLocator/Service/WordFileService.cs index b3edd4afa9923f14f40f00e1d60b8a181d3c32d3..5e16d87f740a1718c10334f38e26b288d7cc5569 100644 --- a/TextLocator/Service/WordFileService.cs +++ b/TextLocator/Service/WordFileService.cs @@ -13,29 +13,37 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { - using (var document = new Document(new FileStream(filePath, FileMode.Open))) + try { - // 提取每个段落的文本 - StringBuilder builder = new StringBuilder(); - foreach (Section section in document.Sections) + using (var document = new Document(new FileStream(filePath, FileMode.Open))) { - foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs) + // 提取每个段落的文本 + StringBuilder builder = new StringBuilder(); + foreach (Section section in document.Sections) { - builder.AppendLine(paragraph.Text); + foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs) + { + builder.AppendLine(paragraph.Text); + } } + document.Close(); + document.Dispose(); + + content = builder.ToString(); } - content = builder.ToString(); } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); + catch (Exception ex) + { + log.Error(ex.Message, ex); + } } return content; } diff --git a/TextLocator/Service/XmlFileService.cs b/TextLocator/Service/XmlFileService.cs index d82444e937b0251870eb14a06dbf9870638fc628..4d6fccf7f303b90a4bb5508ade70e964460ff5ca 100644 --- a/TextLocator/Service/XmlFileService.cs +++ b/TextLocator/Service/XmlFileService.cs @@ -13,22 +13,30 @@ namespace TextLocator.Service { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); + private static object locker = new object(); + public string GetFileContent(string filePath) { // 文件内容 string content = ""; - try + lock (locker) { - using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8)) + try { - content = reader.ReadToEnd(); + using (StreamReader reader = new StreamReader(new FileStream(filePath, FileMode.Open), Encoding.UTF8)) + { + content = reader.ReadToEnd(); + + content = Regex.Replace(content, "\\<.[^<>]*\\>", ""); - content = Regex.Replace(content, "\\<.[^<>]*\\>", ""); + reader.Close(); + reader.Dispose(); + } + } + catch (Exception ex) + { + log.Error(ex.Message, ex); } - } - catch (Exception ex) - { - log.Error(ex.Message, ex); } return content; } diff --git a/TextLocator/TextLocator.csproj b/TextLocator/TextLocator.csproj index ad3b88bfe80880bcbb26f46548ce1369af417c41..4819408c15b102bb60f41fe576e50fc937c6188e 100644 --- a/TextLocator/TextLocator.csproj +++ b/TextLocator/TextLocator.csproj @@ -170,6 +170,11 @@ FolderWindow.xaml + + True + True + Resources.resx + @@ -217,11 +222,6 @@ Code - - True - True - Resources.resx - True Settings.settings @@ -281,7 +281,6 @@ - @@ -291,10 +290,10 @@ - + - + \ No newline at end of file diff --git a/TextLocator/Util/FileUtil.cs b/TextLocator/Util/FileUtil.cs index 58a32c3b5ccc7729bb7397055c39b418b6cfd81f..29314eb550c1264b876088b044729df4acf23f07 100644 --- a/TextLocator/Util/FileUtil.cs +++ b/TextLocator/Util/FileUtil.cs @@ -40,15 +40,21 @@ namespace TextLocator.Util case FileType.PDF类型: bitmap = Properties.Resources.pdf; break; + case FileType.HTML和XML类型: + bitmap = Properties.Resources.html; + break; case FileType.常用图片: - bitmap = Properties.Resources.jpg; + bitmap = Properties.Resources.image; break; case FileType.代码文件: bitmap = Properties.Resources.code; break; - default: + case FileType.纯文本: bitmap = Properties.Resources.txt; break; + default: + bitmap = Properties.Resources.rtf; + break; } BitmapImage bi = new BitmapImage(); using (MemoryStream ms = new MemoryStream())