using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.AccessControl; using System.Security.Cryptography; using System.Text; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2016/1/5 11:37:29 //Author //Description Create //---------------------------------------------------------------- using System.Xml; using System.Xml.Serialization; namespace Muchinfo.MTPClient.Update { public class UpdateHelper { /// /// 获取配置文件路径 /// /// private string GetConfigPath(string applictionUpdateUrl) { string configPath = string.Empty; if (applictionUpdateUrl == null) { return configPath; } var typeinfo = typeof (UpdateInfo); if (applictionUpdateUrl.EndsWith("/")) { configPath = string.Format("{0}{1}", applictionUpdateUrl,typeinfo.Name); } else { configPath = string.Format("{0}/{1}", applictionUpdateUrl, typeinfo.Name); } return configPath; } /// /// 反序列化对象 /// /// 数据流 /// 实体对象 public UpdateInfo DeSerialize(Stream stream) { Type[] types = new Type[] { typeof(UpdateFile) }; var xs = new XmlSerializer(typeof(UpdateInfo), types); return xs.Deserialize(stream) as UpdateInfo; } /// /// 加载升级配置文件 /// /// 升级路径 /// public UpdateInfo LoadUpdateConfig(string updatePath) { var doc = new XmlDocument(); // string loadPath = GetConfigPath(updatePath); doc.Load(updatePath); using (var ms=new System.IO.MemoryStream()) { doc.Save(ms); ms.Flush(); ms.Position = 0; return DeSerialize(ms); } } /// /// 生成本地文件的MD5 /// /// 要生成文件Md5的目录 /// 要排除的项目 public UpdateInfo CreateUpdateInfoFile(string path,string[] removeDir=null) { var updateInfo = new UpdateInfo(); SearchfileCompress(updateInfo,removeDir, path); return updateInfo; } /// /// 生成MD5配置文件 /// /// 配置文件内容 /// 排除的文件夹 /// 父目录路径 private void SearchfileCompress(UpdateInfo updateFile,string[] removeDir, string directoryPath) { var dirInfo = new DirectoryInfo(directoryPath); var files = dirInfo.GetFiles(); //.Where((item) => item.Name != AppDomain.CurrentDomain.FriendlyName); foreach (var file in files) { string path = file.Name; var md5Code = GetFileMD5Code(file); var updatefile = new UpdateFile(path.ToLower(), file.Extension, md5Code); if (updateFile.UpdateFiles == null) { updateFile.UpdateFiles = new List(); } updateFile.UpdateFiles.Add(updatefile); } var directories = dirInfo.GetDirectories(); foreach (var directory in directories) { if (removeDir == null || !removeDir.Where((item)=>item.ToLower().Equals( directory.Name.ToLower())).Any()) { EnumeratorDirectory(updateFile, directory, removeDir, string.Empty, directoryPath); } } } /// /// 递归生成文件配置文件 /// /// 配置文件内容 /// 生成配置文件的目录 /// 不生成的目录 /// 父目录 /// 是否压缩 private void EnumeratorDirectory(UpdateInfo updateFile, DirectoryInfo directory,string[] removeDir, string parentDir, string baseCompessdir) { var files = directory.GetFiles(); string dirpath = parentDir + "\\" + directory.Name; foreach (var file in files) { string path = dirpath + "\\" + file.Name; var md5Code = GetFileMD5Code(file); var updatefile = new UpdateFile(path.ToLower(), file.Extension, md5Code); if (updateFile.UpdateFiles == null) { updateFile.UpdateFiles = new List(); } updateFile.UpdateFiles.Add(updatefile); } var directories = directory.GetDirectories(); foreach (var dirInfo in directories) { if (removeDir == null || !removeDir.Where((item) => item.ToLower().Equals(directory.Name.ToLower())).Any()) { EnumeratorDirectory(updateFile, dirInfo, removeDir, dirpath, baseCompessdir); } } } /// /// 计算文件MD5 /// /// /// private string GetFileMD5Code(FileInfo fileInfo) { try { using (FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open,FileAccess.Read)) { System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider(); byte[] retval = md5.ComputeHash(fs); fs.Close(); var sbBuilder = new StringBuilder(); for (int i = 0; i < retval.Length; i++) { sbBuilder.Append(retval[i].ToString("x2")); } return sbBuilder.ToString().ToUpper(); } } catch (Exception) { return string.Empty; } } /// /// 生成更新文件的XML /// /// 更新配置 /// 保存路径 public void SaveUpdateXml(UpdateInfo updateInfo, string filePath) { var xs = new XmlSerializer(updateInfo.GetType()); using (var stream = new FileStream(filePath, FileMode.Create)) { xs.Serialize(stream, updateInfo); stream.Close(); } } /// /// 生成更新文件的XML /// /// 更新配置 /// 保存路径 public static void SaveStaticUpdateXml(UpdateInfo updateInfo, string filePath) { var xs = new XmlSerializer(updateInfo.GetType()); using (var stream = new FileStream(filePath, FileMode.Create)) { xs.Serialize(stream, updateInfo); stream.Close(); } } } }