FileComparisonHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using PDF_Master.Properties;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Xml;
  11. namespace PDF_Master.Helper
  12. {
  13. public static class FileComparisonHelper
  14. {
  15. #if DEBUG
  16. public static string URI_AppXml = "http://test-pdf-pro.kdan.cn:3021/downloads/pdfreaderprocast_win_en_Us.xml";
  17. #else
  18. public static string URI_AppXml = "https://www.pdfreaderpro.com/downloads/pdfreaderprocast_win_en_Us.xml";
  19. #endif
  20. private static string OCRmodelMD5 = "";
  21. /// <summary>
  22. /// 获取文件MD5
  23. /// </summary>
  24. /// <param name="folderPath">文件路径</param>
  25. /// <returns></returns>
  26. public static string CalculateFileMD5(string filePath)
  27. {
  28. using (var md5 = MD5.Create())
  29. {
  30. using (var stream = File.OpenRead(filePath))
  31. {
  32. var hash = md5.ComputeHash(stream);
  33. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  34. }
  35. }
  36. }
  37. /// <summary>
  38. /// 获取文件夹MD5
  39. /// </summary>
  40. /// <param name="folderPath">文件路径</param>
  41. /// <returns></returns>
  42. public static string CalculateFolderMD5(string folderPath)
  43. {
  44. var files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
  45. using (var md5 = MD5.Create())
  46. {
  47. //遍历文件夹获取,各个文件夹MD5合并为一个MD5
  48. foreach (var file in files)
  49. {
  50. var fileHash = CalculateFileMD5(file);
  51. var hashBytes = Encoding.UTF8.GetBytes(fileHash);
  52. md5.TransformBlock(hashBytes, 0, hashBytes.Length, hashBytes, 0);
  53. }
  54. md5.TransformFinalBlock(new byte[0], 0, 0);
  55. var hash = md5.Hash;
  56. return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
  57. }
  58. }
  59. /// <summary>
  60. /// 判断OCR资源是否存在
  61. /// </summary>
  62. public static bool OCRModelItExist()
  63. {
  64. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  65. if (Directory.Exists(folderPath))
  66. {
  67. Settings.Default.AppProperties.OCRmodelMD5 = "65d21a699138c194f06a8640dd40a901";
  68. Settings.Default.AppProperties.OCRFile_Url = "http://test-pdf-pro.kdan.cn:3021/downloads/OCREngine.zip";
  69. if (!Getpdfreaderprocast())
  70. {
  71. return false;
  72. }
  73. string folderMD5 = FileComparisonHelper.CalculateFolderMD5(folderPath);
  74. if (folderMD5 == Settings.Default.AppProperties.OCRmodelMD5)
  75. {
  76. return true;
  77. }
  78. }
  79. else
  80. {
  81. Console.WriteLine("文件夹不存在");
  82. }
  83. if (!Getpdfreaderprocast())
  84. {
  85. return false;
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// 删除OCR资源
  91. /// </summary>
  92. public static void RemoveOCRModel()
  93. {
  94. try
  95. {
  96. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  97. if (Directory.Exists(folderPath))
  98. {
  99. Directory.Delete(folderPath, true);
  100. }
  101. string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
  102. if (Directory.Exists(folderzipPath))
  103. {
  104. Directory.Delete(folderzipPath, true);
  105. }
  106. }
  107. catch
  108. {
  109. }
  110. }
  111. /// <summary>
  112. /// 获取OCR下载信息
  113. /// </summary>
  114. /// <returns></returns>
  115. public static bool Getpdfreaderprocast()
  116. {
  117. if (string.IsNullOrEmpty(Settings.Default.AppProperties.OCRFile_Url) && string.IsNullOrEmpty(Settings.Default.AppProperties.OCRmodelMD5))
  118. {
  119. string folder = System.IO.Path.Combine(App.CurrentPath, "AppXml");
  120. DirectoryInfo directory = new DirectoryInfo(folder);
  121. try
  122. {
  123. if (!directory.Exists)
  124. {
  125. directory.Create();
  126. }
  127. string FileNameAppxml = folder + "\\" + "appxml" + ".xml";
  128. //下载What`s new
  129. WebClient client = new WebClient();
  130. client.DownloadFile(URI_AppXml, FileNameAppxml);
  131. XmlDocument xmlDoc = new XmlDocument();
  132. xmlDoc.Load(FileNameAppxml);
  133. XmlNode root = xmlDoc.DocumentElement;
  134. //获取OCR信息父节点
  135. XmlNode itemNodes = root.SelectSingleNode("channel/item/ocrenclosure");
  136. //获取OCR信息子节点个数
  137. int siblingCount = 0;
  138. foreach (XmlNode itemNode in itemNodes)
  139. {
  140. XmlNode parentNode = itemNode.ParentNode;
  141. siblingCount = parentNode.ChildNodes.Count;
  142. }
  143. //遍历子节点
  144. for (int i = 0; i < siblingCount; i++)
  145. {
  146. //对应子节点对象
  147. XmlNode itemNode = root.SelectSingleNode("channel/item/ocrenclosure/ocrenclosure" + i);
  148. XmlAttribute ocrstartversionUrlAttribute = itemNode.Attributes["sparkle:startversion"];
  149. XmlAttribute ocrendversionUrlAttribute = itemNode.Attributes["sparkle:endversion"];
  150. //版本号区间
  151. Version ocrendversion = new Version(ocrendversionUrlAttribute.Value);
  152. Version ocrstartversion = new Version(ocrstartversionUrlAttribute.Value);
  153. Version Appversion = new Version(App.Version);
  154. if (ocrstartversion <= Appversion && Appversion <= ocrendversion)
  155. {
  156. //获取OCR下载链接
  157. XmlAttribute ocrUrlAttribute = itemNode.Attributes["ocrurl"];
  158. Settings.Default.AppProperties.OCRFile_Url = ocrUrlAttribute?.Value;
  159. //获取OCRMD5
  160. XmlAttribute ocrUrlAttributemd5 = itemNode.Attributes["ocrmd5"];
  161. Settings.Default.AppProperties.OCRmodelMD5 = ocrUrlAttributemd5?.Value;
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. catch { return false; }
  168. }
  169. return true;
  170. }
  171. }
  172. }