| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- 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
- {
- /// <summary>
- /// 获取配置文件路径
- /// </summary>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 反序列化对象
- /// </summary>
- /// <param name="stream">数据流</param>
- /// <returns>实体对象</returns>
- public UpdateInfo DeSerialize(Stream stream)
- {
- Type[] types = new Type[] { typeof(UpdateFile) };
- var xs = new XmlSerializer(typeof(UpdateInfo), types);
- return xs.Deserialize(stream) as UpdateInfo;
- }
- /// <summary>
- /// 加载升级配置文件
- /// </summary>
- /// <param name="updatePath">升级路径</param>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// 生成本地文件的MD5
- /// </summary>
- /// <param name="path">要生成文件Md5的目录</param>
- /// <param name="removeDir">要排除的项目</param>
- public UpdateInfo CreateUpdateInfoFile(string path,string[] removeDir=null)
- {
- var updateInfo = new UpdateInfo();
- SearchfileCompress(updateInfo,removeDir, path);
- return updateInfo;
- }
-
- /// <summary>
- /// 生成MD5配置文件
- /// </summary>
- /// <param name="updateFile">配置文件内容</param>
- /// <param name="removeDir">排除的文件夹</param>
- /// <param name="directoryPath">父目录路径</param>
- 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>();
- }
- 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);
- }
- }
- }
-
- /// <summary>
- /// 递归生成文件配置文件
- /// </summary>
- /// <param name="updateFile">配置文件内容</param>
- /// <param name="directory">生成配置文件的目录</param>
- /// <param name="removeDir">不生成的目录</param>
- /// <param name="parentDir">父目录</param>
- /// <param name="baseCompessdir">是否压缩</param>
- 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>();
- }
- 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);
- }
- }
- }
- /// <summary>
- /// 计算文件MD5
- /// </summary>
- /// <param name="fileInfo"></param>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 生成更新文件的XML
- /// </summary>
- /// <param name="updateInfo">更新配置</param>
- /// <param name="filePath">保存路径</param>
- 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();
- }
- }
- /// <summary>
- /// 生成更新文件的XML
- /// </summary>
- /// <param name="updateInfo">更新配置</param>
- /// <param name="filePath">保存路径</param>
- 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();
- }
- }
- }
- }
|