-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileDeDuplicator.cs
51 lines (46 loc) · 1.43 KB
/
FileDeDuplicator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace HtmlExporterPlugin
{
class FileDeDuplicator
{
private Dictionary<string, string> DuplicateDictionary = new Dictionary<string, string>();
private static string CalculateMD5(string filename)
{
using (var md5 = MD5.Create())
{
using (var stream = new BufferedStream(File.OpenRead(filename), 1048576))
{
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
}
}
public void clear()
{
DuplicateDictionary.Clear();
}
public string GetUniqueFile(string fullfilename, string storedfilename, bool DoCheck)
{
if (!DoCheck || fullfilename.Equals(String.Empty) || !File.Exists(fullfilename))
{
return storedfilename;
}
string md5 = CalculateMD5(fullfilename);
if (DuplicateDictionary.ContainsKey(md5))
{
return DuplicateDictionary[md5];
}
else
{
DuplicateDictionary[md5] = storedfilename;
return storedfilename;
}
}
}
}