using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Mime; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; //---------------------------------------------------------------- //Module Name: $safeprojectname$ //Purpose: //CopyRight: Muchinfo //History: //---------------------------------------------------------------- //DateTime 2015/12/30 9:12:33 //Author //Description Create //---------------------------------------------------------------- using System.Xml.Serialization; namespace Muchinfo.Client.Update.UpdateListFactory { public class FilesHelper { private const string CompassPath = "MuchinfoUpdate"; public static void SearchfileCompress(UpdateInfo updateFile ,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); if (updateFile.IsCompress) { Console.WriteLine("正在压缩:" + file.FullName); using (FileStream fStream = new FileStream(file.FullName, FileMode.Open)) { var fileDirectoy = directoryPath + "\\" + CompassPath + "\\"; if (!Directory.Exists(fileDirectoy)) { Directory.CreateDirectory(fileDirectoy); } ZipCompessHelper.CreateCompressFile(fStream, fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz"); fStream.Close(); } } var updatefile = new UpdateFile(path, file.Extension, md5Code); if (updateFile.UpdateFiles == null) { updateFile.UpdateFiles = new List(); } updateFile.UpdateFiles.Add(updatefile); } var directories = dirInfo.GetDirectories(); var directoryList= directories.Where((dir) => dir.Name != CompassPath); //删除压缩的 foreach (var directory in directoryList) { EnumeratorDirectory(updateFile, directory, string.Empty, directoryPath); } } /// /// 递归读取文件 /// /// /// /// /// public static void EnumeratorDirectory(UpdateInfo updateFile, DirectoryInfo directory,string parentDir,string baseCompessdir) { var files = directory.GetFiles(); string dirpath = parentDir + "\\" + directory.Name; foreach (var file in files) { string path = dirpath+"\\" + file.Name; if (updateFile.IsCompress) { Console.WriteLine("正在压缩:" + file.FullName); using (FileStream fStream = new FileStream(file.FullName, FileMode.Open)) { var fileDirectoy = baseCompessdir + "\\" + CompassPath + "\\" + dirpath + "\\"; if (!Directory.Exists(fileDirectoy)) { Directory.CreateDirectory(fileDirectoy); } ZipCompessHelper.CreateCompressFile(fStream, fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz"); fStream.Close(); } } var md5Code = GetFileMD5Code(file); var updatefile = new UpdateFile(path, file.Extension, md5Code); if (updateFile.UpdateFiles == null) { updateFile.UpdateFiles = new List(); } updateFile.UpdateFiles.Add(updatefile); } var directories = directory.GetDirectories(); foreach (var dirInfo in directories) { EnumeratorDirectory(updateFile, dirInfo, dirpath, baseCompessdir); } } /// /// 只做当前文件夹下所有文件夹更新 /// /// /// public static void CurrentDirectory(UpdateInfo updateFile, string directoryPath) { var dirInfo = new DirectoryInfo(directoryPath); var directoryList = dirInfo.GetDirectories(); foreach (var directory in directoryList) { var files = directory.GetFiles(); foreach (var file in files) { string path = directory.Name+ "\\" + file.Name; var md5Code = GetFileMD5Code(file); if (updateFile.IsCompress) { Console.WriteLine("正在压缩:" + file.FullName); using (FileStream fStream = new FileStream(file.FullName, FileMode.Open)) { var fileDirectoy = directoryPath + "\\" + CompassPath + "\\" + directory.Name+"\\"; if (!Directory.Exists(fileDirectoy)) { Directory.CreateDirectory(fileDirectoy); } ZipCompessHelper.CreateCompressFile(fStream, fileDirectoy + file.Name.Replace(file.Extension, string.Empty) + ".gz"); fStream.Close(); } } var updatefile = new UpdateFile(path, file.Extension, md5Code); // updatefile.Directory = directory.Name; if (updateFile.UpdateFiles == null) { updateFile.UpdateFiles = new List(); } updateFile.UpdateFiles.Add(updatefile); } } } /// /// 计算文件MD5 /// /// /// public static string GetFileMD5Code(FileInfo fileInfo) { using (FileStream fs= File.OpenRead(fileInfo.FullName)) { 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(); } } /// /// 生成更新文件的XML /// /// 更新配置 /// 保存路径 public static void SaveUpdateXml(UpdateInfo updateInfo, string filePath) { Type[] types = new Type[] { typeof(UpdateFile) }; var xs = new XmlSerializer(updateInfo.GetType(), types); using (var stream = new FileStream(filePath, FileMode.Create)) { xs.Serialize(stream, updateInfo); stream.Close(); } } } }