Skip to content

Instantly share code, notes, and snippets.

@RobThree
Created December 11, 2013 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobThree/7913383 to your computer and use it in GitHub Desktop.
Save RobThree/7913383 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Mind == Blown

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