Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
}
}
@RobThree

This comment has been minimized.

Copy link
Owner Author

@RobThree RobThree commented Dec 11, 2013

Mind == Blown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment