Example #1
0
        /// <summary>
        /// Compares the file contents for two directories and returns information about the differences.
        /// </summary>
        /// <param name="source">The source directory to compare to the destination.</param>
        /// <param name="destination">The destination directory where, ultimately, files would be copied.</param>
        /// <returns>A <see cref="ComparisonResult"/> object that contains the result of the comparison operation.</returns>
        /// <exception cref="DirectoryNotFoundException"></exception>
        public override ComparisonResult Compare(string source, string destination)
        {
            base.Scan(source, destination);

            var    modifiedFiles   = new List <string>();
            string sourceHash      = null;
            string destinationHash = null;

            // compare the overlap files
            foreach (var file in base.FilesToCompare)
            {
                using (var sourceFile = new FileStream(string.Concat(source, file), FileMode.Open, FileAccess.Read))
                {
                    using (var destinationFile = new FileStream(string.Concat(destination, file), FileMode.Open, FileAccess.Read))
                    {
                        using (var cryptoProvider = new SHA1CryptoServiceProvider())
                        {
                            sourceHash      = BitConverter.ToString(cryptoProvider.ComputeHash(sourceFile));
                            destinationHash = BitConverter.ToString(cryptoProvider.ComputeHash(destinationFile));

                            // if the hashes don't match, the files are different. add them to the return set.
                            if (!sourceHash.Equals(destinationHash, StringComparison.OrdinalIgnoreCase))
                            {
                                modifiedFiles.Add(string.Concat(source, file));
                            }
                        }
                    }
                }
            }

            var newFiles         = new List <string>(base.NewFiles);
            var notInSourceFiles = new List <string>(base.NotInSource);
            var result           = new ComparisonResult(newFiles, modifiedFiles, notInSourceFiles);

            return(result);
        }
        /// <summary>
        /// Compares the file contents for two directories and returns information about the differences.
        /// </summary>
        /// <param name="source">The source directory to compare to the destination.</param>
        /// <param name="destination">The destination directory where, ultimately, files would be copied.</param>
        /// <returns>A <see cref="ComparisonResult"/> object that contains the result of the comparison operation.</returns>
        /// <exception cref="DirectoryNotFoundException"></exception>
        public override ComparisonResult Compare(string source, string destination)
        {
            base.Scan(source, destination);

            var modifiedFiles = new List<string>();
            string sourceHash = null;
            string destinationHash = null;

            // compare the overlap files
            foreach (var file in base.FilesToCompare)
            {
                using (var sourceFile = new FileStream(string.Concat(source, file), FileMode.Open, FileAccess.Read))
                {
                    using (var destinationFile = new FileStream(string.Concat(destination, file), FileMode.Open, FileAccess.Read))
                    {
                        using (var cryptoProvider = new SHA1CryptoServiceProvider())
                        {
                            sourceHash = BitConverter.ToString(cryptoProvider.ComputeHash(sourceFile));
                            destinationHash = BitConverter.ToString(cryptoProvider.ComputeHash(destinationFile));

                            // if the hashes don't match, the files are different. add them to the return set.
                            if (!sourceHash.Equals(destinationHash, StringComparison.OrdinalIgnoreCase))
                            {
                                modifiedFiles.Add(string.Concat(source, file));
                            }
                        }
                    }
                }
            }

            var newFiles = new List<string>(base.NewFiles);
            var notInSourceFiles = new List<string>(base.NotInSource);
            var result = new ComparisonResult(newFiles, modifiedFiles, notInSourceFiles);

            return result;
        }
        /// <summary>
        /// Compares the file contents for two directories and returns information about the differences.
        /// </summary>
        /// <param name="source">The source directory to compare to the destination.</param>
        /// <param name="destination">The destination directory where, ultimately, files would be copied.</param>
        /// <returns>A <see cref="ComparisonResult"/> object that contains the result of the comparison operation.</returns>
        /// <exception cref="DirectoryNotFoundException"></exception>
        public override ComparisonResult Compare(string source, string destination)
        {
            base.Scan(source, destination);

            var modifiedFiles = new List<string>();

            byte[] sourceBytes;
            byte[] destinationBytes;
            BinaryReader sourceReader = null;
            BinaryReader destinationReader = null;

            // compare the overlap files
            foreach (var file in base.FilesToCompare)
            {
                using (var sourceFile = new FileStream(string.Concat(source, file), FileMode.Open, FileAccess.Read))
                {
                    using (var destinationFile = new FileStream(string.Concat(destination, file), FileMode.Open, FileAccess.Read))
                    {
                        if (sourceFile.Length.Equals(destinationFile.Length))
                        {
                            // if they are the same size, compare them.
                            sourceReader = new BinaryReader(sourceFile);
                            destinationReader = new BinaryReader(destinationFile);
                            do
                            {
                                sourceBytes = sourceReader.ReadBytes(ByteStreamComparer.BufferSize);
                                destinationBytes = destinationReader.ReadBytes(ByteStreamComparer.BufferSize);

                                if (sourceBytes.Length > 0)
                                {
                                    // if the arrays of bytes aren't equal, then the files are different. add them to the return set.
                                    if (!sourceBytes.ByteArrayCompare(destinationBytes))
                                    {
                                        modifiedFiles.Add(string.Concat(source, file));
                                        break;
                                    }
                                }
                            } while (sourceBytes.Length > 0);
                        }
                        else
                        {
                            // if the files aren't the same size, obviously they are different. add them to the return set.
                            modifiedFiles.Add(string.Concat(source, file));
                        }
                    }
                }
            }

            var newFiles = new List<string>(base.NewFiles);
            var notInSourceFiles = new List<string>(base.NotInSource);
            var result = new ComparisonResult(newFiles, modifiedFiles, notInSourceFiles);

            return result;
        }
        private void HandleNotInSourceFiles(ComparisonResult result, string rootPath)
        {
            var notInSourceFiles = new List<ITaskItem>();
            base.Log.LogMessage(MessageImportance.High, "Files that exist on destination but not source", null);

            this.HandleFiles(result.NotInSource, notInSourceFiles, rootPath);

            this.NotInSourceFiles = notInSourceFiles.ToArray();
        }
        private void HandleNewFiles(ComparisonResult result, string rootPath)
        {
            var newFiles = new List<ITaskItem>();
            base.Log.LogMessage(MessageImportance.High, "New Files", null);

            this.HandleFiles(result.NewFiles, newFiles, rootPath);

            this.NewFiles = newFiles.ToArray();
        }