I'm trying to keep files in sync over network. Instead of transferring the whole file on each change, I'd like to find and compress the changed parts and transfer only those, very similar to e.g. subversion. Is there a reliable C# solution to this problem, or do I have to write it myself?
-
I'd prefer not having to use external programs. – mafu Mar 10 '11 at 13:00
3 Answers
The key word is "diff algorithm". There are several implementations in c#.
Diff.NET and A Generic, Reusable Diff Algorithm in C# looks like reasonable choices. Google will find you another couple of hundred choices.
Be aware that most diff algorithms are specially crafted to handle line based text files.
If you want to diff binary data or parts of lines you need to be more careful when picking a diff library.
If you want to compress the diff too you can use the System.IO.Compression.GZipStream
class.

- 46,430
- 8
- 69
- 108
This post may help you.
In general you are looking for a way to binary-compare the new file to the old file and then strip out just the changed bytes, compress them, transfer them to the other system, decompress them and insert them into the file at the appropriate locations. So it sounds like you'll need an application running on each end (perhaps the same application if you want it to work both ways).
-
Yes, I'm going to run the app on both machines, just as you described. The linked post looks very interesting. – mafu Mar 10 '11 at 13:03
-
in which format do you have the changes? byte[], string or stream?
have a look here: Compress and decompress strings in C#

- 43,984
- 10
- 98
- 147
-
I do not yet have the changes, that's part of the question. :) The files themselves are byte[] – mafu Mar 10 '11 at 13:00