Created
December 11, 2013 16:16
-
-
Save RobThree/7913383 to your computer and use it in GitHub Desktop.
See this topic: http://gathering.tweakers.net/forum/list_messages/1568620 (Google translated version: http://riii.nl/qm7te )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var duplicates = new DirectoryInfo(@"G:\sampling") | |
.GetFiles("*.*", SearchOption.AllDirectories) | |
.GroupBy(f => f, new FileComparer()) //Group by file equality | |
.Where(g => g.Count() > 1); //Groups with more than 1 file | |
//Voila! | |
} | |
} | |
public class FileComparer : IEqualityComparer<FileInfo> | |
{ | |
public bool Equals(FileInfo left, FileInfo right) | |
{ | |
if (left.Length != right.Length) | |
return false; | |
Int64 l = 0, r = 0; //"Buffers" | |
long p = 0, //Position | |
i = left.Length / 8, //Iterations of "int64-sized reads" | |
c = left.Length % 8; //Rest | |
using (var brl = new BinaryReader(left.OpenRead())) | |
using (var brr = new BinaryReader(right.OpenRead())) | |
{ | |
//Compare "int64 chunks" at a time | |
while (l == r && p++ < i) | |
{ | |
l = brl.ReadInt64(); | |
r = brr.ReadInt64(); | |
} | |
//Compare remaining bytes (if any and the left still equalled right in the last read) | |
if (l == r && c > 0) | |
{ | |
p = 0; //Reset position | |
while (l == r && p++ < c) | |
{ | |
l = brl.ReadByte(); | |
r = brr.ReadByte(); | |
} | |
} | |
} | |
return l == r; | |
} | |
public int GetHashCode(FileInfo obj) | |
{ | |
return unchecked((int)obj.Length); //As a first "indicator" the filelength is used as "hashcode" | |
} | |
} |
Author
RobThree
commented
Dec 11, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment