Mark C

Mark C

  • NA
  • 4
  • 3.3k

Help with file patching.

Apr 8 2011 10:32 PM
Here is my code to create a patch file.

FileStream fs1 = new FileStream(sourceFile, FileMode.Open, FileAccess.Read);
  FileStream fs2 = new FileStream(targetFile, FileMode.Open, FileAccess.Read);
  List<int> diffOffsets = new List<int>();
  List<byte> diffBytes = new List<byte>();
  int extraBytes = (int)(fs2.Length - fs1.Length);
  for (int d = 0; d <= fs1.Length - 1; d++)
  {
  byte b1 = (byte)fs1.ReadByte();
  byte b2 = (byte)fs2.ReadByte();
  if (b1 != b2)
  {
  diffOffsets.Add((int)fs1.Position);
  diffBytes.Add(b2);
  }
  }
  if (extraBytes > 0)
  {
  for (int d = 0; d <= extraBytes; d++)
  {
  diffOffsets.Add((int)fs2.Position);
  diffBytes.Add((byte)fs2.ReadByte());
  }
  }
  FileStream patchFS = new FileStream(patchFile, FileMode.Create, FileAccess.Write);
  StreamWriter patchSW = new StreamWriter(patchFS);
  for (int d = 0; d <= diffOffsets.Count - 1; d++)
  {
  patchSW.WriteLine(diffOffsets[d].ToString() + "|" + diffBytes[d].ToString());
  }
  fs1.Close();
  fs2.Close();
  patchSW.Close();

Here is my code to apply a patch file.
 FileStream fs1 = new FileStream(targetFile, FileMode.Open, FileAccess.ReadWrite);
  string[] patchContents = File.ReadAllLines(patchFile);
  for (int d = 0; d <= patchContents.Count() - 1; d++)
  {
  string[] data = patchContents[d].Split("|".ToCharArray());
  fs1.Position = Convert.ToInt32(data[0]);
  fs1.WriteByte(ToHex(data[1]));
  }
  fs1.Flush();
  byte[] newBytes = new byte[fs1.Length];
  fs1.Read(newBytes, 0, (int)fs1.Length);
  File.WriteAllBytes(targetFile + "NEW", newBytes);

Here is my ToHex function.
        private byte ToHex(string val)
        {
            return (byte)Int32.Parse(val, System.Globalization.NumberStyles.HexNumber);
        }        
Why I apply the patch file, how come File1 is different from File2? (MD5 Hashes do not match)

Answers (1)