FileComparisonHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 = "";
  74. if (string.IsNullOrEmpty(OCRmodelMD5))
  75. {
  76. folderMD5 = FileComparisonHelper.CalculateFolderMD5(folderPath);
  77. }
  78. else {
  79. folderMD5 = OCRmodelMD5;
  80. }
  81. if (folderMD5 == Settings.Default.AppProperties.OCRmodelMD5)
  82. {
  83. OCRmodelMD5 = folderMD5;
  84. return true;
  85. }
  86. }
  87. else
  88. {
  89. Console.WriteLine("文件夹不存在");
  90. }
  91. if (!Getpdfreaderprocast())
  92. {
  93. return false;
  94. }
  95. return false;
  96. }
  97. /// <summary>
  98. /// 删除OCR资源
  99. /// </summary>
  100. public static void RemoveOCRModel()
  101. {
  102. try
  103. {
  104. string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
  105. if (Directory.Exists(folderPath))
  106. {
  107. Directory.Delete(folderPath, true);
  108. }
  109. string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
  110. if (Directory.Exists(folderzipPath))
  111. {
  112. Directory.Delete(folderzipPath, true);
  113. }
  114. }
  115. catch
  116. {
  117. }
  118. }
  119. /// <summary>
  120. /// 获取OCR下载信息
  121. /// </summary>
  122. /// <returns></returns>
  123. public static bool Getpdfreaderprocast()
  124. {
  125. if (string.IsNullOrEmpty(Settings.Default.AppProperties.OCRFile_Url) && string.IsNullOrEmpty(Settings.Default.AppProperties.OCRmodelMD5))
  126. {
  127. string folder = System.IO.Path.Combine(App.CurrentPath, "AppXml");
  128. DirectoryInfo directory = new DirectoryInfo(folder);
  129. try
  130. {
  131. if (!directory.Exists)
  132. {
  133. directory.Create();
  134. }
  135. string FileNameAppxml = folder + "\\" + "appxml" + ".xml";
  136. //下载What`s new
  137. WebClient client = new WebClient();
  138. client.DownloadFile(URI_AppXml, FileNameAppxml);
  139. XmlDocument xmlDoc = new XmlDocument();
  140. xmlDoc.Load(FileNameAppxml);
  141. XmlNode root = xmlDoc.DocumentElement;
  142. //获取OCR信息父节点
  143. XmlNode itemNodes = root.SelectSingleNode("channel/item/ocrenclosure");
  144. //获取OCR信息子节点个数
  145. int siblingCount = 0;
  146. foreach (XmlNode itemNode in itemNodes)
  147. {
  148. XmlNode parentNode = itemNode.ParentNode;
  149. siblingCount = parentNode.ChildNodes.Count;
  150. }
  151. //遍历子节点
  152. for (int i = 0; i < siblingCount; i++)
  153. {
  154. //对应子节点对象
  155. XmlNode itemNode = root.SelectSingleNode("channel/item/ocrenclosure/ocrenclosure" + i);
  156. XmlAttribute ocrstartversionUrlAttribute = itemNode.Attributes["sparkle:startversion"];
  157. XmlAttribute ocrendversionUrlAttribute = itemNode.Attributes["sparkle:endversion"];
  158. //版本号区间
  159. Version ocrendversion = new Version(ocrendversionUrlAttribute.Value);
  160. Version ocrstartversion = new Version(ocrstartversionUrlAttribute.Value);
  161. Version Appversion = new Version(App.Version);
  162. if (ocrstartversion <= Appversion && Appversion <= ocrendversion)
  163. {
  164. //获取OCR下载链接
  165. XmlAttribute ocrUrlAttribute = itemNode.Attributes["ocrurl"];
  166. Settings.Default.AppProperties.OCRFile_Url = ocrUrlAttribute?.Value;
  167. //获取OCRMD5
  168. XmlAttribute ocrUrlAttributemd5 = itemNode.Attributes["ocrmd5"];
  169. Settings.Default.AppProperties.OCRmodelMD5 = ocrUrlAttributemd5?.Value;
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. catch { return false; }
  176. }
  177. return true;
  178. }
  179. }
  180. }