/// <summary> /// 获取已经包含变更状态的同步文件结构文件 /// </summary> /// <param name="synDir">同步的结构目录</param> /// <param name="oldSynFile">就的同步结构文件</param> /// <param name="ignoreHiddenFile">是否忽略隐藏文件</param> /// <param name="ignoreHiddenFolder">是否忽略隐藏文件夹</param> /// <param name="msgRpt">指定消息回报处理方式</param> /// <param name="progressRpt">指定进度处理方式</param> /// <returns>所有文件的结构性同步文件</returns> public static SynchronizationFile GetSynFileChangeState(string synDir, SynchronizationFile oldSynFile, bool ignoreHiddenFile, bool ignoreHiddenFolder, ReportMessage msgRpt, ReportProgress progressRpt) { SynchronizationFile newSynFile = new SynchronizationFile(); using (FileSystemWorker fSysWorker = new FileSystemWorker(synDir, new BuildSynFileWorker(synDir, newSynFile, oldSynFile))) { string siteRoot = AppDomain.CurrentDomain.BaseDirectory; //设置忽视隐藏文件夹或文件 fSysWorker.FileExclude = f => { if (ignoreHiddenFile) { return(f.FullName.EndsWith(".syn", StringComparison.InvariantCultureIgnoreCase) || (f.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden); } else { return(f.FullName.EndsWith(".syn", StringComparison.InvariantCultureIgnoreCase)); } }; fSysWorker.IgnoreHiddenFolder = ignoreHiddenFolder; fSysWorker.MsgReport = msgRpt; fSysWorker.ProgressReport = progressRpt; fSysWorker.Execute(); } if (oldSynFile != null) { //比较旧结构不同,找出删除文件 SetNotExistFileIn(newSynFile, oldSynFile, f => { newSynFile.RemoveFiles.Add(f.FileID, f); }); //修复只是重命名的文件,而不是删除了重新添加的一个文件。 newSynFile.RenameList = FixedRename(newSynFile.RemoveFiles, newSynFile.AddFiles); } newSynFile.SynTimeUTC = DateTime.Now.ToUniversalTime(); return(newSynFile); }
static void BuildSynFile(string baseDir, string targetFileName) { SynchronizationFile synOld = null; SynchronizationFile synFile = new SynchronizationFile(); string sKey = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7"; SymmetricAlgorithm mobjCryptoService = new RijndaelManaged(); byte[] key = GetLegalKey(mobjCryptoService, sKey); byte[] iv = GetLegalIV(mobjCryptoService); SymmetricCryptography <RijndaelManaged> CryProvider = new SymmetricCryptography <RijndaelManaged>(key, iv); if (File.Exists(targetFileName)) { byte[] eBytes = File.ReadAllBytes(targetFileName); eBytes = CryProvider.Decrypt(eBytes); synOld = eBytes.GetObject <SynchronizationFile>(); } using (FileSystemWorker fSysWorker = new FileSystemWorker(baseDir, new BuildSynFileWorker(baseDir, synFile, synOld))) { string siteRoot = AppDomain.CurrentDomain.BaseDirectory; //设置忽视隐藏文件夹或文件 fSysWorker.FileExclude = f => { //return (f.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden; return(f.FullName.EndsWith(".syn", StringComparison.InvariantCultureIgnoreCase)); }; //fSysWorker.IgnoreHiddenFolder = true; fSysWorker.MsgReport = str => Console.WriteLine(string.Format("{0}", str.Replace(siteRoot, "").Replace(baseDir, "").Replace("\\", "/"))); fSysWorker.ProgressReport = p => Progress(string.Format("已完成{0}", Regex.Replace((p * 100).ToString("0.00") + "%...", "\\.0{1,}(%...)$", "%"))); fSysWorker.Execute(); } synFile.SynTimeUTC = DateTime.Now.ToUniversalTime(); if (synOld == null) { File.WriteAllBytes(targetFileName, CryProvider.Encrypt(synFile.GetBytes())); } else { //比较旧结构不同,找出删除文件 GlobalUtil.SetNotExistFileIn(synFile, synOld, f => { synFile.RemoveFiles.Add(f.FileID, f); }); //修复只是重命名的文件,而不是删除了重新添加的一个文件。 synFile.RenameList = GlobalUtil.FixedRename(synFile.RemoveFiles, synFile.AddFiles); //Create Patch //File.WriteAllBytes(targetFileName, CryProvider.Encrypt(synFile.GetBytes())); } Progress(".目录{0}\r\n已创建同步文件为{1}", baseDir, targetFileName); Progress("同步文件信息:共{0}文件,大小{1}字节!\r\n + {2}, * {3}, - {4}, R {5}", synFile.TotalFileCount, synFile.TotalFileSize, synFile.AddFiles.Count, synFile.UpdateFiles.Count, synFile.RemoveFiles.Count, synFile.RenameList.Length ); #region 详细变化情况 if (synFile.AddFiles.Count > 0) { foreach (SynFile aFile in synFile.AddFiles.Values) { Progress("添加了文件:{0}, 文件大小:{1}bytes", aFile.FileID, aFile.FileSize); } } if (synFile.UpdateFiles.Count > 0) { foreach (SynFile uFile in synFile.UpdateFiles.Values) { Progress("修改了文件:{0}, 文件大小:{1}bytes", uFile.FileID, uFile.FileSize); } } if (synFile.RemoveFiles.Count > 0) { foreach (SynFile dFile in synFile.RemoveFiles.Values) { Progress("删除了文件:{0}, 文件大小:{1}bytes", dFile.FileID, dFile.FileSize); } } if (synFile.RenameList.Length > 0) { for (int i = 0, j = synFile.RenameList.Length; i < j; i++) { Progress("重命名{0}为{1}", synFile.RenameList[i].OldSynFile.FileID, synFile.RenameList[i].NewSynFile.FileID); } } #endregion }
public void FileSystemWithOptionTest() { /* * ReadOnly Hidden System Normal Encrypted */ SynOption option = new SynOption(); option.MatchRules = new SynRule[] { //new FileNameRule { Setting = "/\\.(cs|js|aspx)$/", OrBeforeRule = false } //,new FileNameRule { Setting = "*.html", OrBeforeRule = true } //, new FileSizeRule { Setting = "(0-3k]", OrBeforeRule = false } , new FileAttributeRule { Setting = "ReadOnly|Hidden", OrBeforeRule = false } }; option.ExcludeRules = new SynRule[] { new FileNameRule { Setting = "*.rar", OrBeforeRule = false } , new FileSizeRule { Setting = ">1M", OrBeforeRule = true } , new FolderNameRule { Setting = "obj", OrBeforeRule = true } , new FolderNameRule { Setting = "bin", OrBeforeRule = true } }; FileRuleBase[] filematchRules = SynRule.GetSubRuleArrayIn <FileRuleBase>(option.MatchRules); string synDir = @"D:\DevRoot\EaseV2\EaseV1_3\www"; SynFileItemWorker synWorker = new SynFileItemWorker(synDir, option); using (FileSystemWorker fSysWorker = new FileSystemWorker(synDir, synWorker)) { string siteRoot = AppDomain.CurrentDomain.BaseDirectory; //通过匹配模式查找 fSysWorker.FileMatch = f => { int iLast = -1; bool blnResult = false; foreach (FileRuleBase fRule in filematchRules) { if (iLast == -1) { iLast += 1; blnResult = fRule.Actived(f); } else { if (fRule.OrBeforeRule) { blnResult = blnResult || fRule.Actived(f); } else { blnResult = blnResult && fRule.Actived(f); } } } return(blnResult); }; //忽略文件的判断 fSysWorker.FileExclude = ifile => { return(false); }; //忽略文件夹的判断 fSysWorker.FolderExclude = fd => { return(true); }; //fSysWorker.OnSkipFileActive = s => //{ // Console.Error.WriteLine("skip {0}", s.FullName); //}; fSysWorker.MsgReport = s => System.Diagnostics.Trace.WriteLine(s); //fSysWorker.ProgressReport = p => Console.WriteLine(string.Format("已完成{0}", // Regex.Replace((p * 100).ToString("0.00") + "%...", "\\.0{1,}(%...)$", "%"))); fSysWorker.Execute(); } }