FileComparisonHelper.cs 7.3 KB

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