123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading.Tasks;
- namespace vCloud.Windows.ForceUpgrade
- {
- public class UpgradeFilePathReader
- {
- private Dictionary<string, UpgradeFileInfo> _fileVersionDic = new Dictionary<string, UpgradeFileInfo>();
- private string _rootPath;
- private void ReaderFiles(string path)
- {
- var files = Directory.GetFiles(path);
- foreach (var file in files)
- {
- try
- {
- var fileInfo = new FileInfo(file);
- var fileVersionInfo = FileVersionInfo.GetVersionInfo(file);
- var lastWriteTime = fileInfo.LastWriteTimeUtc;
- var fileSize = fileInfo.Length;
- var fileVersion = lastWriteTime.ToString("yyyyMMddHHmmss") + "." + fileSize;
- var productVersion = fileVersionInfo.FileVersion;
- var fileShortPath = file.Replace(_rootPath.TrimEnd('\\') + "\\", "RootPath\\");
- _fileVersionDic.Add(fileShortPath, new UpgradeFileInfo
- {
- FileVersion = fileVersion,
- ProductVersion = productVersion,
- Md5Code = GetMd5(file),
- LastWriteTime = lastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"),
- Size = fileSize
- });
- }
- catch (Exception ex)
- {
- FileHelper.AppendWriteFile("InstallLog.txt", ex.Message + "|" + ex.StackTrace);
- }
- }
- var dirs = Directory.GetDirectories(path);
- if (dirs != null && dirs.Length > 0)
- {
- foreach(var dir in dirs)
- {
- ReaderFiles(dir);
- }
- }
- }
- private string GetMd5(string FilePath)
- {
- var file = File.ReadAllBytes(FilePath);
- var md5 = System.Security.Cryptography.MD5.Create();
- var md5Str = BitConverter.ToString(md5.ComputeHash(file)).Replace("-", "");
- return md5Str;
- }
- private object Clone(object obj)
- {
- using (MemoryStream memoryStream = new MemoryStream())
- {
- BinaryFormatter formatter = new BinaryFormatter();
- formatter.Serialize(memoryStream, obj);
- memoryStream.Position = 0;
- return formatter.Deserialize(memoryStream);
- }
- }
- /// <summary>
- /// 获取文件版本字典
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public Dictionary<string, UpgradeFileInfo> StartReader(string path)
- {
- _fileVersionDic.Clear();
- _rootPath = path;
- ReaderFiles(path);
- return Clone(_fileVersionDic) as Dictionary<string, UpgradeFileInfo>;
- }
- /// <summary>
- /// 比对不同文件夹,文件版本差异,生成更新文件列表
- /// </summary>
- /// <param name="path"></param>
- /// <param name="diffPath"></param>
- /// <returns></returns>
- public Dictionary<string, UpgradeFileInfo> DiffReader(string path, string diffPath)
- {
- var dic = StartReader(path);
- var diffDic = StartReader(diffPath);
- foreach (var fileInfo in diffDic)
- {
- if (dic.Keys.Contains(fileInfo.Key))
- {
- var fileValue = dic[fileInfo.Key];
- fileValue.Type = UpgradeOperateType.Add;
- if (fileValue.Size == fileInfo.Value.Size && fileValue.ProductVersion == fileInfo.Value.ProductVersion)
- {
- dic.Remove(fileInfo.Key);
- }
- }
- else
- {
- fileInfo.Value.Type = UpgradeOperateType.Remove;
- dic.Add(fileInfo.Key, fileInfo.Value);
- }
- }
- return dic;
- }
- /// <summary>
- /// 比对不同文件夹生成的文件版本列表,生成更新文件列表
- /// </summary>
- /// <param name="json"></param>
- /// <param name="diffJson"></param>
- /// <returns></returns>
- public Dictionary<string, UpgradeFileInfo> DiffReaderJson(string json, string diffJson)
- {
- var dic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(json);
- var diffDic = JsonConvert.DeserializeObject<Dictionary<string, UpgradeFileInfo>>(diffJson);
- foreach (var fileInfo in diffDic)
- {
- if (dic.Keys.Contains(fileInfo.Key))
- {
- var fileValue = dic[fileInfo.Key];
- fileValue.Type = UpgradeOperateType.Add;
- if (fileValue.Size == fileInfo.Value.Size && fileValue.ProductVersion == fileInfo.Value.ProductVersion)
- {
- dic.Remove(fileInfo.Key);
- }
- }
- else
- {
- fileInfo.Value.Type = UpgradeOperateType.Remove;
- dic.Add(fileInfo.Key, fileInfo.Value);
- }
- }
- return dic;
- }
- }
- }
|