123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using PDF_Master.Properties;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- namespace PDF_Master.Helper
- {
- public static class FileComparisonHelper
- {
- #if DEBUG
- public static string URI_AppXml = "http://test-pdf-pro.kdan.cn:3021/downloads/pdfreaderprocast_win_en_Us.xml";
- #else
- public static string URI_AppXml = "https://www.pdfreaderpro.com/downloads/pdfreaderprocast_win_en_Us.xml";
- #endif
- private static string OCRmodelMD5 = "";
- /// <summary>
- /// 获取文件MD5
- /// </summary>
- /// <param name="folderPath">文件路径</param>
- /// <returns></returns>
- public static string CalculateFileMD5(string filePath)
- {
- using (var md5 = MD5.Create())
- {
- using (var stream = File.OpenRead(filePath))
- {
- var hash = md5.ComputeHash(stream);
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- }
- /// <summary>
- /// 获取文件夹MD5
- /// </summary>
- /// <param name="folderPath">文件路径</param>
- /// <returns></returns>
- public static string CalculateFolderMD5(string folderPath)
- {
- var files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories);
- using (var md5 = MD5.Create())
- {
- //遍历文件夹获取,各个文件夹MD5合并为一个MD5
- foreach (var file in files)
- {
- var fileHash = CalculateFileMD5(file);
- var hashBytes = Encoding.UTF8.GetBytes(fileHash);
- md5.TransformBlock(hashBytes, 0, hashBytes.Length, hashBytes, 0);
- }
- md5.TransformFinalBlock(new byte[0], 0, 0);
- var hash = md5.Hash;
- return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
- }
- }
- /// <summary>
- /// 判断OCR资源是否存在
- /// </summary>
- public static bool OCRModelItExist()
- {
- string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
- if (Directory.Exists(folderPath))
- {
- Settings.Default.AppProperties.OCRmodelMD5 = "65d21a699138c194f06a8640dd40a901";
- Settings.Default.AppProperties.OCRFile_Url = "http://test-pdf-pro.kdan.cn:3021/downloads/OCREngine.zip";
- if (!Getpdfreaderprocast())
- {
- return false;
- }
- string folderMD5 = "";
- if (string.IsNullOrEmpty(OCRmodelMD5))
- {
- folderMD5 = FileComparisonHelper.CalculateFolderMD5(folderPath);
- }
- else {
- folderMD5 = OCRmodelMD5;
- }
- if (folderMD5 == Settings.Default.AppProperties.OCRmodelMD5)
- {
- OCRmodelMD5 = folderMD5;
- return true;
- }
- }
- else
- {
- Console.WriteLine("文件夹不存在");
- }
- if (!Getpdfreaderprocast())
- {
- return false;
- }
- return false;
- }
- /// <summary>
- /// 删除OCR资源
- /// </summary>
- public static void RemoveOCRModel()
- {
- try
- {
- string folderPath = System.IO.Path.Combine(App.CurrentPath, "OCREngine");
- if (Directory.Exists(folderPath))
- {
- Directory.Delete(folderPath, true);
- }
- string folderzipPath = System.IO.Path.Combine(App.CurrentPath, "OCREnginezip");
- if (Directory.Exists(folderzipPath))
- {
- Directory.Delete(folderzipPath, true);
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 获取OCR下载信息
- /// </summary>
- /// <returns></returns>
- public static bool Getpdfreaderprocast()
- {
- if (string.IsNullOrEmpty(Settings.Default.AppProperties.OCRFile_Url) && string.IsNullOrEmpty(Settings.Default.AppProperties.OCRmodelMD5))
- {
- string folder = System.IO.Path.Combine(App.CurrentPath, "AppXml");
- DirectoryInfo directory = new DirectoryInfo(folder);
- try
- {
- if (!directory.Exists)
- {
- directory.Create();
- }
- string FileNameAppxml = folder + "\\" + "appxml" + ".xml";
- //下载What`s new
- WebClient client = new WebClient();
- client.DownloadFile(URI_AppXml, FileNameAppxml);
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(FileNameAppxml);
- XmlNode root = xmlDoc.DocumentElement;
- //获取OCR信息父节点
- XmlNode itemNodes = root.SelectSingleNode("channel/item/ocrenclosure");
- //获取OCR信息子节点个数
- int siblingCount = 0;
- foreach (XmlNode itemNode in itemNodes)
- {
- XmlNode parentNode = itemNode.ParentNode;
- siblingCount = parentNode.ChildNodes.Count;
- }
- //遍历子节点
- for (int i = 0; i < siblingCount; i++)
- {
- //对应子节点对象
- XmlNode itemNode = root.SelectSingleNode("channel/item/ocrenclosure/ocrenclosure" + i);
- XmlAttribute ocrstartversionUrlAttribute = itemNode.Attributes["sparkle:startversion"];
- XmlAttribute ocrendversionUrlAttribute = itemNode.Attributes["sparkle:endversion"];
- //版本号区间
- Version ocrendversion = new Version(ocrendversionUrlAttribute.Value);
- Version ocrstartversion = new Version(ocrstartversionUrlAttribute.Value);
- Version Appversion = new Version(App.Version);
- if (ocrstartversion <= Appversion && Appversion <= ocrendversion)
- {
- //获取OCR下载链接
- XmlAttribute ocrUrlAttribute = itemNode.Attributes["ocrurl"];
- Settings.Default.AppProperties.OCRFile_Url = ocrUrlAttribute?.Value;
- //获取OCRMD5
- XmlAttribute ocrUrlAttributemd5 = itemNode.Attributes["ocrmd5"];
- Settings.Default.AppProperties.OCRmodelMD5 = ocrUrlAttributemd5?.Value;
- return true;
- }
- }
- return false;
- }
- catch { return false; }
- }
- return true;
- }
- }
- }
|