| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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>();
- }
- 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);
- }
- }
- /// <summary>
- /// 递归读取文件
- /// </summary>
- /// <param name="updateFile"></param>
- /// <param name="directory"></param>
- /// <param name="parentDir"></param>
- /// <param name="baseCompessdir"></param>
- 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>();
- }
- updateFile.UpdateFiles.Add(updatefile);
- }
- var directories = directory.GetDirectories();
- foreach (var dirInfo in directories)
- {
- EnumeratorDirectory(updateFile, dirInfo, dirpath, baseCompessdir);
- }
-
- }
- /// <summary>
- /// 只做当前文件夹下所有文件夹更新
- /// </summary>
- /// <param name="updateFile"></param>
- /// <param name="directoryPath"></param>
- 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>();
- }
- updateFile.UpdateFiles.Add(updatefile);
- }
- }
- }
- /// <summary>
- /// 计算文件MD5
- /// </summary>
- /// <param name="fileInfo"></param>
- /// <returns></returns>
- 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();
- }
- }
- /// <summary>
- /// 生成更新文件的XML
- /// </summary>
- /// <param name="updateInfo">更新配置</param>
- /// <param name="filePath">保存路径</param>
- 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();
- }
- }
-
- }
- }
|