-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilelist.java
More file actions
executable file
·403 lines (351 loc) · 11.3 KB
/
Filelist.java
File metadata and controls
executable file
·403 lines (351 loc) · 11.3 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.security.MessageDigest;
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;
transient private int groupTotal = 0;
transient private int outerGroup = 0;
transient private int innerGroup = 0;
transient private long bytesTotal = 0;
transient private long bytesRead = 0;
transient private int hashTotal = 0;
transient private int hashCurrent = 0;
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; }
private String calculateFileHash(File file) {
try (InputStream is = Files.newInputStream(file.toPath())) {
MessageDigest md5Digest = MessageDigest.getInstance("MD5");
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
md5Digest.update(buffer, 0, bytesRead);
}
byte[] digest = md5Digest.digest();
// Convert byte array to a hex string to use as a Map key
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (Exception e) {
// Return a unique string for files that can't be read,
// so they don't get grouped with anything else.
return "ERROR_" + file.getAbsolutePath();
}
}
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("");
}
}
public String getStatus ()
{
//int count = countfiles();
StringBuilder sb = new StringBuilder();
sb.append("Comparing files: ");
sb.append(filesProcessed);
sb.append("/");
sb.append(countfiles());
if (hashTotal>0)
{
sb.append(" Hashing: (");
sb.append(hashCurrent);
sb.append("/");
sb.append(hashTotal);
sb.append(")");
}
if (groupTotal>0)
{
sb.append(" Group: (");
sb.append(outerGroup);
sb.append("/");
sb.append(groupTotal);
sb.append(") (");
sb.append(innerGroup);
sb.append("/");
sb.append(groupTotal);
sb.append(")");
}
if (bytesTotal>0)
{
sb.append(" Bytes read: ");
sb.append(bytesRead);
sb.append("/");
sb.append(bytesTotal);
}
return sb.toString();
}
// 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 {
bytesTotal = Files.size(file1);
final long size = Files.size(file1);
// zero length check
if (size < 4096) {
bytesTotal=0;
return Arrays.equals(Files.readAllBytes(file1), Files.readAllBytes(file2));
}
/* Chunked compare
*
* 15 try (InputStream is1 = Files.newInputStream(file1);
16 InputStream is2 = Files.newInputStream(file2)) {
17
18 // 1. Create two separate buffers. 8192 (8KB) is a good, common size.
19 byte[] buffer1 = new byte[8192];
20 byte[] buffer2 = new byte[8192];
21
22 while (true) {
23 // 2. Read a chunk from each file.
24 int bytesRead1 = is1.read(buffer1);
25 int bytesRead2 = is2.read(buffer2);
26
27 // 3. Check if the number of bytes read is the same.
28 // If one stream ends before the other, they are not identical.
29 if (bytesRead1 != bytesRead2) {
30 return false;
31 }
32
33 // 4. If we've reached the end of both files (EOF), they are identical so far.
34 if (bytesRead1 == -1) {
35 return true;
36 }
37
38 // 5. Compare the contents of the buffers.
39 // We must only compare up to the number of bytes actually read.
40 for (int i = 0; i < bytesRead1; i++) {
41 if (buffer1[i] != buffer2[i]) {
42 return false;
43 }
44 }
45
46 // 6. Update our progress tracker with the size of the chunk.
47 this.bytesCompared += bytesRead1;
48 }
49 }
*/
bytesRead = 0;
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)
{
bytesRead++;
if (data != is2.read())
{
bytesTotal=0;
return false;
}
}
}
bytesTotal = 0;
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.
bytesTotal=0;
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<String>hashFiles = new ArrayList<String>();
hashTotal = samesize.size();
hashCurrent = 0;
for(File f : samesize)
{
hashCurrent++;
hashFiles.add(calculateFileHash(f));
}
hashTotal = 0;
groupTotal = samesize.size();
boolean[] alreadyGrouped = new boolean[samesize.size()];
for (int i =0; i < hashFiles.size(); i++)
{
outerGroup=i;
//System.err.println("" + i + "/" + hashFiles.size() + " multicompare.");
if (!alreadyGrouped[i])
{
ArrayList<File> samelist = new ArrayList<File>();
samelist.add(samesize.get(i));
for (int j=i+1; j < hashFiles.size(); j++)
{
innerGroup=j;
if (!alreadyGrouped[j])
{
if (hashFiles.get(i).equals(hashFiles.get(j)))
{
if (sameContent(samesize.get(i),samesize.get(j)))
{
samelist.add(samesize.get(j));
alreadyGrouped[i] = true;
alreadyGrouped[j] = true;
}
}
}
}
if (samelist.size() > 1)
duplicatelist.add(samelist);
}
}
groupTotal = 0;
innerGroup=0;
outerGroup=0;
/*
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];
if (!Files.isSymbolicLink(sourcefile.toPath()))
//System.out.println(sourcefile.getAbsolutePath());
if (sourcefile.isFile()) {
Long fsize = 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();
}
}