So, i don\'t have a single idea to get through this situation i have a function that patches a file by replacing the values i want but he file i am trying to patch is about 4.5G
Your problem is that File.ReadAllBytes
will not open files with lengths longer than Int.MaxValue
. Loading an entire file into memory just to scan it for a pattern is a bad design no matter how big the file is. You should open the file as a stream and use the Scanner pattern to step through the file, replacing bytes that match your pattern. A rather simplistic implementation using BinaryReader
:
static void PatchStream(Stream source, Stream target
, IList<byte> searchPattern, IList<byte> replacementPattern)
{
using (var input = new BinaryReader(source))
using (var output = new BinaryWriter(target))
{
var buffer = new Queue<byte>();
while (true)
{
if (buffer.Count < searchPattern.Count)
{
if (input.BaseStream.Position < input.BaseStream.Length)
buffer.Enqueue(input.ReadByte());
else
break;
}
else if (buffer.Zip(searchPattern, (b, s) => b == s).All(c => c))
{
foreach (var b in replacementPattern)
output.Write(b);
buffer.Clear();
}
else
{
output.Write(buffer.Dequeue());
}
}
foreach (var b in buffer)
output.Write(b);
}
}
You can call it on files with code like:
PatchStream(new FileInfo(...).OpenRead(),
new FileInfo(...).OpenWrite(),
new[] { (byte)'a', (byte)'b', (byte)'c' },
new[] { (byte)'A', (byte)'B', (byte)'C' });