-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (87 loc) · 1.9 KB
/
main.go
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
var (
dataPath = "./"
//fileName = "test.txt"
)
type FileInfo struct {
Name string
MD5 string
}
func main() {
if len(os.Args) > 1 {
dataPath = os.Args[1]
}
files, _ := grabDirectory(dataPath)
searchDuplicates(files)
}
func grabDirectory(dataPath string) ([]string, error) {
//fmt.Printf("Scan from dir - %s\n", dataPath)
files, err := ioutil.ReadDir(dataPath)
if err != nil {
fmt.Printf("error occurred with a ReadDir: %v", err.Error())
}
var filesArray []string
for _, file := range files {
filePath := filepath.Join(dataPath, file.Name())
if file.IsDir() {
files, err := grabDirectory(filePath)
if err != nil {
return nil, err
}
filesArray = append(filesArray, files...)
}
//if filepath.Ext(strings.TrimSpace(filePath)) == ".txt" {
filesArray = append(filesArray, filePath)
//}
}
return filesArray, nil
}
func getMD5SumString(file *FileInfo) (string, error) {
f, err := os.Open(file.Name)
if err != nil{
return "", err
}
defer f.Close()
md5 := md5.New()
_, err = io.Copy(md5, f)
if err != nil{
return "", err
}
file.MD5 = fmt.Sprintf("%X", md5.Sum(nil))
return file.MD5, nil
}
func searchDuplicates(files []string) {
data := map[int64][]*FileInfo{}
for _, file := range files{
fileinfo, _ := os.Stat(file)
data[fileinfo.Size()] = append(data[fileinfo.Size()], &FileInfo{Name: file})
}
hashes := map[string][]*FileInfo{}
for _, list := range data{
if len(list) > 1 {
for _, file := range list{
if hash, err := getMD5SumString(file); hash != "" && err == nil{
hashes[hash] = append(hashes[hash], file)
}
}
for _, list := range hashes{
if len(list) > 1{
fmt.Print("Files that have the same content: ")
var str []string
for _, file := range list{
str = append(str, file.Name)
}
fmt.Println(str)
}
}
}
}
}