-
Notifications
You must be signed in to change notification settings - Fork 0
/
Filelist.java
executable file
·232 lines (190 loc) · 6.5 KB
/
Filelist.java
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import java.io.*;
import java.util.*;
import java.nio.file.*;
public class Filelist implements Runnable, Serializable {
transient private HashMap<Long,HashSet<File>> flist;
private ArrayList<ArrayList<File>> duplicatelist;
// private File scanDir;
private ArrayList<File>scanDirs;
transient private ArrayList<File> toProcess;
transient private int filesProcessed =0;
transient private int matchesProcessed =0;
transient private boolean scanCompleted = false;
transient private boolean compareCompleted = false;
public Filelist (File dir) { this(); addScanDir(dir); }
public Filelist (String dir) { this(); addScanDir(dir); }
public Filelist ()
{
scanDirs = new ArrayList<File>();
flist = new HashMap<Long,HashSet<File>>();
duplicatelist = new ArrayList<ArrayList<File>>();
}
//public void setScanDir (File dir) { scanDir = dir; }
// public void setScanDir (String dir) { scanDir = new File(dir); }
// public File getScanDir () { return scanDir; }
public void addScanDir(File dir) { scanDirs.add(dir); }
public void addScanDir(String dir) { scanDirs.add(new File(dir)); }
// these are needed for serializable...?
public ArrayList<ArrayList<File>> getDuplicatelist() { return duplicatelist; }
public void setDuplicatelist(ArrayList<ArrayList<File>> dl) { duplicatelist = dl; }
public void print()
{
for (ArrayList<File> al : duplicatelist)
{
boolean first = true;
for (File fl : al)
{
if(!first)
System.out.print(" : ");
first=false;
try {
System.out.print(fl.getCanonicalPath());
} catch (Exception e) {
System.out.print("*ERROR*");
}
}
System.out.println("");
}
}
// from https://stackoverflow.com/questions/27379059/determine-if-two-files-store-the-same-content
private boolean sameContent(File file1, File file2) {
return sameContent (file1.toPath(), file2.toPath());
}
private boolean sameContent(Path file1, Path file2) {
try {
final long size = Files.size(file1);
// zero length check
if (size < 4096)
return Arrays.equals(Files.readAllBytes(file1), Files.readAllBytes(file2));
try (BufferedInputStream is1 = new BufferedInputStream (Files.newInputStream(file1));
BufferedInputStream is2 = new BufferedInputStream(Files.newInputStream(file2))) {
// Compare byte-by-byte.
// Note that this can be sped up drastically by reading large chunks
// (e.g. 16 KBs) but care must be taken as InputStream.read(byte[])
// does not neccessarily read a whole array!
int data;
while ((data = is1.read()) != -1)
if (data != is2.read())
return false;
}
return true;
} catch (IOException e) {
// if one file has a read error, it's probably not going to be the same as the other file
// or it's best not to match it for other reasons.
return false;
}
}
// public int countfiles() { return flist.size(); }
public int countfiles()
{
int total = 0;
for (Map.Entry<Long, HashSet<File>> fe : flist.entrySet())
total += fe.getValue().size()>1? 1:0;
return total;
}
public int getProcessed() { return filesProcessed; }
public int getMatches() { return matchesProcessed; }
public int getScanRemain() {
if (toProcess != null)
return toProcess.size();
return 0;
}
public boolean getScanCompleted() { return scanCompleted; }
public boolean getCompareCompleted() { return compareCompleted; }
// compare a list of files
public void filecompare()
{
// some sort of readable progress
filesProcessed = 0;
compareCompleted = false;
for (Map.Entry<Long, HashSet<File>> fe : flist.entrySet()) {
ArrayList<File> samesize = new ArrayList<File>(fe.getValue());
if (samesize.size() > 1) {
filesProcessed ++;
//System.err.println("files: " + samesize.size() + " bytes: " + fe.getKey());
if (samesize.size() == 2)
{
if (sameContent(samesize.get(0),samesize.get(1)))
{
matchesProcessed ++;
ArrayList<File> samelist = new ArrayList<File>();
samelist.add(samesize.get(0));
samelist.add(samesize.get(1));
duplicatelist.add(samelist);
}
// filesProcessed ++;
//System.err.println("processed: " + filesProcessed); // tp be handled in a different thread
} else {
ArrayList<File> duplicateFiles = new ArrayList<File>(samesize);
while (duplicateFiles.size() > 0)
{
ArrayList<File> samelist = new ArrayList<File>();
samelist.add(duplicateFiles.get(0));
for (int j =1; j < duplicateFiles.size(); j++)
{
if (sameContent(duplicateFiles.get(0),duplicateFiles.get(j)))
{
matchesProcessed ++;
samelist.add(duplicateFiles.get(j));
duplicateFiles.remove(j);
}
// System.err.println("processed: " + filesProcessed); // tp be handled in a different thread
}
// filesProcessed ++;
if (samelist.size() > 1)
duplicatelist.add(samelist);
duplicateFiles.remove(0);
}
}
}
}
compareCompleted = true;
}
// create a list of file, index by filesize
public void filescan()
{
scanCompleted = false;
toProcess = new ArrayList<File>(scanDirs);
// System.out.println("toProcess: " + toProcess.get(0));
// toProcess.add(getScanDir());
//System.out.println("size: " + toProcess.size() + " item[0]: " + toProcess.get(0) +"<");
// System.out.println("filelist: " + toProcess.get(0).listFiles());
while (toProcess.size() > 0) {
// System.out.println("Processing... " + toProcess.get(0));
//System.out.println("toprocess: " + toProcess.size());
File testist[] = testist = toProcess.get(0).listFiles(); // why is this returning null..?
if (testist != null) {
// System.out.println("container array type: " + testist);
// System.out.println("file list count: " + testist.length);
for (int idx=0; idx<testist.length; ++idx) {
File sourcefile = testist[idx];
//System.out.println(sourcefile.getAbsolutePath());
if (sourcefile.isFile()) {
Long fsize = new Long(sourcefile.length());
if (fsize > 0) // use find -empty
{
if (flist.containsKey(fsize))
{
flist.get(fsize).add(sourcefile);
} else {
HashSet<File> hs = new HashSet<File>();
hs.add(sourcefile);
flist.put(fsize,hs);
}
}
} else if ( sourcefile.isDirectory()) {
toProcess.add(sourcefile);
} else {
System.out.println ("Source is not a file or directory. " + sourcefile.getAbsolutePath());
}
}
}
toProcess.remove(0);
}
scanCompleted = true;
}
public void run() {
filescan();
filecompare();
}
}