/// <summary> /// 校验MD5 /// </summary> private void validateMD5() { string origin = Header.MD5; StringBuilder hash = new StringBuilder(); Header.MD5 = Writer.SECRET_KEY; hash.Append(MD5Helper.ComputeHash(BinaryHelper.Serialize(Header))); for (int i = 0; i < Header.Count; i++) { using (FileStream fs = new FileStream(Path.Combine(WorkDirectory, Header[i].FileName), FileMode.Open)) { hash.Append(MD5Helper.ComputeHash(fs)); } } string current = MD5Helper.ComputeHash(hash.ToString()); if (origin != current) { //throw new Exception("MD5校验值不一致,文件已被修改"); } Header.MD5 = origin; }
public void Append(string name, object content) { if (name == "header") { throw new Exception($"header为保留关键字,不允许使用。"); } if (Header.Where(item => item.Name == name).Count() > 0) { throw new Exception($"已存在同名的数据项{name}"); } byte[] data = BinaryHelper.Serialize(content); if (Header.EnablePage) { if (currentPos + data.Length > Header.MaxSize * 1024 * 1024) { fs.Close(); currentPage++; fs = new FileStream(Path.Combine(WorkDirectory, $"{Header.FileName}._{currentPage}"), FileMode.CreateNew); fileList.Add(fs.Name); currentPos = fs.Position; } } Header.Add(new DataEntry { Name = name, PageNum = currentPage, Offset = currentPos, Length = data.Length }); fs.Write(data, 0, data.Length); currentPos = fs.Position; }
private void writeHeader() { fs = new FileStream(Path.Combine(WorkDirectory, $"{Header.FileName}.header"), FileMode.CreateNew); fileList.Add(fs.Name); byte[] data = BinaryHelper.Serialize(Header); fs.Write(data, 0, data.Length); fs.Close(); }
/// <summary> /// 将头部信息写入文件 /// </summary> private void writeHeader() { string partialName = $"{Header.FileName}.header"; addPartialFile(partialName); FileStream fs = new FileStream(Path.Combine(WorkDirectory, partialName), FileMode.CreateNew); byte[] data = BinaryHelper.Serialize(Header); fs.Write(data, 0, data.Length); fs.Close(); }
/// <summary> /// /// </summary> private void computeMD5() { StringBuilder hash = new StringBuilder(); Header.MD5 = SECRET_KEY; hash.Append(MD5Helper.ComputeHash(BinaryHelper.Serialize(Header))); for (int i = 0; i < Header.Count; i++) { using (FileStream fs = new FileStream(Path.Combine(WorkDirectory, Header[i].FileName), FileMode.Open)) { hash.Append(MD5Helper.ComputeHash(fs)); } } Header.MD5 = MD5Helper.ComputeHash(hash.ToString()); }