Check DataInspectorDemo project for the examples;

The code below check for the string "demo" inside a file, insert "modified" text before "demo" and then save the output to another file.
Despite may be seems a useless practice, can be useful when handling large data (i.e a file with size > 1Gb).

static void SaveFile() {

	System.Diagnostics.Debug.WriteLine("SaveFile");

	string filename = "demo.txt";

	MemoryStream ms = new MemoryStream();

	using (FileStream fs = new FileStream(filename, FileMode.Open))
	{
		DIUtils.CopyStream(fs, ms, fs.Length, (pos, tot) =>
		{
			System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));
		});

		ms.Position = 0;
	}

	using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(ms))
	{
		string str2find = "demo";

		byte[] data = System.Text.Encoding.UTF8.GetBytes(str2find);

		long index = inspector.Find(data, null);

		inspector.Stream.Position = index;

		byte[] data2Insert = System.Text.Encoding.UTF8.GetBytes("modified ");

		inspector.Insert(data2Insert, null);

		string output = @"D:\Temp\demo_mod.txt";

		DIUtils.SaveToFile(inspector.Stream, output, (pos, tot) =>
		{
			System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));
		});
	}
}

Last edited Apr 20, 2016 at 2:35 PM by Zenon66, version 2