public static byte[] CreatePatchFor(string filename1, string filename2)
        {
            // Compute hashes
            HashBlock[] hashBlocksFromReceiver;
            using (FileStream sourceStream = File.Open(filename1, FileMode.Open))
            {
                hashBlocksFromReceiver = new HashBlockGenerator(new RollingChecksum(),
                                                                new HashAlgorithmWrapper<MD5>(MD5.Create()),
                                                                blockSize).ProcessStream(sourceStream).ToArray();
            }

            // Compute deltas
            MemoryStream deltaStream = new MemoryStream();
            using (FileStream fileStream = File.Open(filename2, FileMode.Open))
            {
                DeltaGenerator deltaGen = new DeltaGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()));
                deltaGen.Initialize(blockSize, hashBlocksFromReceiver);
                IEnumerable<IDelta> deltas = deltaGen.GetDeltas(fileStream);
                deltaGen.Statistics.Dump();
                fileStream.Seek(0, SeekOrigin.Begin);
                DeltaStreamer streamer = new DeltaStreamer();
                streamer.Send(deltas, fileStream, deltaStream);
            }

            return deltaStream.ToArray();
        }
 public void ProcessStream_returns_correct_block_count()
 {
     MemoryStream stream = GetOriginalFileStream();
     var gen = new HashBlockGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()), BLOCK_SIZE);
     Assert.AreEqual(
         Math.Ceiling(originalFile.Length/(float) BLOCK_SIZE),
         gen.ProcessStream(stream).Count());
 }
Exemple #3
0
        static void Main(string[] args)
        {
            int blockSize = 512;

            // Compute hashes
            IEnumerable<HashBlock> hashBlocksFromReceiver;
            using (FileStream sourceStream = File.Open("../../dest.bmp", FileMode.Open))
            {
                hashBlocksFromReceiver = new HashBlockGenerator(new RollingChecksum(),
                                                                new HashAlgorithmWrapper<MD5>(MD5.Create()),
                                                                blockSize).ProcessStream(sourceStream).ToArray();
            }

            // Stream the hash blocks
            var hashBlockStream = new MemoryStream();
            HashBlockStreamer.Stream(hashBlocksFromReceiver, hashBlockStream);

            // Receive the hash block stream
            hashBlockStream.Seek(0, SeekOrigin.Begin);
            Console.Out.WriteLine("Hash block stream length: {0}", hashBlockStream.Length);
            hashBlocksFromReceiver = HashBlockStreamer.Destream(hashBlockStream);

            // Compute deltas
            var deltaStream = new MemoryStream();
            using (FileStream fileStream = File.Open("../../source.bmp", FileMode.Open))
            {
                var deltaGen = new DeltaGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()));
                deltaGen.Initialize(blockSize, hashBlocksFromReceiver);
                IEnumerable<IDelta> deltas = deltaGen.GetDeltas(fileStream);
                deltaGen.Statistics.Dump();
                fileStream.Seek(0, SeekOrigin.Begin);
                var streamer = new DeltaStreamer();
                streamer.Send(deltas, fileStream, deltaStream);
                Console.Out.WriteLine("Delta stream length: {0}", deltaStream.Length);
            }

            // Rewind the delta stream (obviously wouldn't apply from a network pipe)
            deltaStream.Seek(0, SeekOrigin.Begin);

            // Reconstruct file
            using (FileStream sourceStream = File.Open("../../dest.bmp", FileMode.Open))
            {
                using (FileStream outStream = File.Open("../../reconstructed.bmp", FileMode.Create))
                {
                    var streamer = new DeltaStreamer();
                    streamer.Receive(deltaStream, sourceStream, outStream);
                    outStream.Close();
                }
            }
        }
 public void ProcessStream_returns_hashes_with_correct_md5_sums()
 {
     MemoryStream stream = GetOriginalFileStream();
     var gen = new HashBlockGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()), BLOCK_SIZE);
     int i = 0;
     Assert.IsTrue(gen.ProcessStream(stream).All(x =>
                                                 BitConverter.ToString(x.Hash)
                                                     .Replace("-", "")
                                                     .ToLower()
                                                     .Equals(md5sums[i++].ToLower())
                       ));
 }
 public void ProcessStream_returns_hashes_with_correct_length()
 {
     MemoryStream stream = GetOriginalFileStream();
     var gen = new HashBlockGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()), BLOCK_SIZE);
     int i = 0;
     Assert.IsTrue(gen.ProcessStream(stream).All(x =>
                                                 x.Length ==
                                                 Math.Min(BLOCK_SIZE, originalFile.Length - i++*BLOCK_SIZE)));
 }
 public void ProcessStream_throws_for_null_stream()
 {
     var gen = new HashBlockGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()), BLOCK_SIZE);
     gen.ProcessStream(null).Count();
 }
 public void ProcessStream_returns_hashes_with_correct_rolling_sums()
 {
     MemoryStream stream = GetOriginalFileStream();
     var gen = new HashBlockGenerator(new RollingChecksum(), new HashAlgorithmWrapper<MD5>(MD5.Create()), BLOCK_SIZE);
     int i = 0;
     Assert.IsTrue(gen.ProcessStream(stream).All(x => x.Checksum == checkSums[i++]));
 }