public void Save2File(string fileName, string ss) { byte[] dataByte = Encoding.GetEncoding("UTF-8").GetBytes(ss); string md5 = MD5Utils.GetMD5Base64(dataByte); //Debug.Log("Save2File:" + fileName + " md5:" + md5 + "\n" + ss); string length = md5.Length.ToString().PadLeft(4, '0'); ss = length + md5 + ss; //Debug.Log("Save File:" + fileName + " md5:" + md5 + "\n" + ss); FileUtils.CreateTextFile(GetFilePath(fileName), ss); }
/// <summary> /// 检查保存文件的完整性 /// </summary> /// <returns></returns> public bool CheckSaveFileMD5() { Debug.Log("开始检查保存文件的完整性"); if (Directory.Exists(GetFileDir())) { string[] filePaths = PathUtils.GetDirectoryFilePath(GetFileDir()); foreach (var path in filePaths) { string fileName = Path.GetFileNameWithoutExtension(path); string md5 = null; string text = GetFileTextData(fileName, out md5); //Debug.Log("fileName:" + fileName + " md5:" + md5 + "\n" + text); Dictionary <string, string> fileContent = null; if (allRecords.ContainsKey(fileName)) { fileContent = allRecords[fileName]; } else { fileContent = converter.String2Object <Dictionary <string, string> >(text); if (fileContent == null) { fileContent = new Dictionary <string, string>(); } allRecords.Add(fileName, fileContent); } if (!string.IsNullOrEmpty(md5)) { byte[] dataByte = Encoding.GetEncoding("UTF-8").GetBytes(text); //Debug.Log("dataByte.lenth:" + dataByte.Length); string md5New = MD5Utils.GetMD5Base64(dataByte); // string md5New = MD5Utils.GetObjectMD5(text); if (md5New != md5) { Debug.LogError("文件:" + fileName + " md5不正确:" + md5 + " md5New:" + md5New + "\n" + text); return(false); } } else { if (text != null && text.Length < 3) { return(false); } } } } return(true); }