public static bool IsDuplicate(string fileName1, string fileName2) { if (new FileInfo(fileName1).Length != new FileInfo(fileName2).Length) { return(false); } if (HashTool.QuickHashFile(fileName1) != HashTool.QuickHashFile(fileName2)) { return(false); } return(HashTool.HashFile(fileName1) == HashTool.HashFile(fileName2)); }
private string AddToFullHash(FileItem fileItem) { // for small files, the full hash is same as quickhash fileItem.FullHash = fileItem.Size < 4 ? fileItem.QuickHash : HashTool.HashFile(fileItem.FileName); if (_sameFullHashGroups.ContainsKey(fileItem.FullHash)) { // fullhash exists, so duplicate file _sameFullHashGroups[fileItem.FullHash].Files.Add(fileItem); return(fileItem.FullHash); } else { // fullhash did not exist, so no duplicate file _sameFullHashGroups.Add(fileItem.FullHash, new SameFullHashGroup()); _sameFullHashGroups[fileItem.FullHash].Files.Add(fileItem); return(null); } }
// ******** Private functions ****** / private string GetHash(string fileName) { var fileInfo = new FileInfo(fileName); // Add all files in groups based on size if (!_sameSizeGroups.ContainsKey(fileInfo.Length)) { return(null); } if (_sameSizeGroups[fileInfo.Length].Files.Count == 1 && string.IsNullOrEmpty(_sameSizeGroups[fileInfo.Length].Files[0].QuickHash)) { // Single file in group, so no quickhash comparison done. Create one now var prevFileItem = _sameSizeGroups[fileInfo.Length].Files[0]; AddToQuickHash(prevFileItem); } var quickHash = HashTool.QuickHashFile(fileName); if (!_sameQuickHashGroups.ContainsKey(quickHash)) { return(null); } if (_sameQuickHashGroups[quickHash].Files.Count == 1 && string.IsNullOrEmpty(_sameQuickHashGroups[quickHash].Files[0].FullHash)) { // quickhash in group, but no fullhash comparison done. Create one now var prevFileItem = _sameQuickHashGroups[quickHash].Files[0]; AddToFullHash(prevFileItem); } var fullHash = HashTool.HashFile(fileName); if (!_sameFullHashGroups.ContainsKey(fullHash)) { return(null); } return(fullHash); //return !_sameFullHashGroups.ContainsKey(fullHash) ? null : fullHash; }