-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.java
More file actions
206 lines (180 loc) · 5.85 KB
/
Problem2.java
File metadata and controls
206 lines (180 loc) · 5.85 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
/*********************************************************************************************************
*
* Author: Maksim Markov, e-mail: maksim.markov.bg@gmail.com , mobile 088 6 839 991
* Date: 16.02.2015
*
**********************************************************************************************************/
package com.hackbulgaria.tasks;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.HashMap;
import java.util.Arrays;
import java.nio.file.Path;
import java.nio.file.Files;
import java.io.File;
import java.nio.file.DirectoryStream;
import java.nio.file.Paths;
import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.io.IOException;
public class Problem2 {
private List<String> getFileNames(List<String> fileNames, Path dir){
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir))
{
for (Path path : stream) {
if(path.toFile().isDirectory()) getFileNames(fileNames, path);
else {
fileNames.add(path.toAbsolutePath().toString());
}
}
} catch(IOException e){
System.out.println("Exception is thrown: "+e);
e.printStackTrace();
}
return fileNames;
}
private List<String> checkFiles(List<String> fileList) {
List<String> list = new ArrayList<>();
List<String> fileForCheck = new LinkedList<>();
List<String> duplicate = new ArrayList<>();
List<String> mainFile = new ArrayList<>();
Map<Double, List<String>> fileMap = new HashMap<>();
// 1 - Get file size and put in Map with size Key
Double fileSize;
for(String fStr : fileList){
fileSize = new Double(new File(fStr).length());
if(fileMap.containsKey(fileSize)) {
List<String> fileString = fileMap.get(fileSize);
fileString.add(fStr);
fileMap.put(fileSize, fileString);
} else {
List<String> fileStr = new ArrayList<>();
fileStr.add(fStr);
fileMap.put(fileSize, fileStr);
}
}
// 2 - Compare files by first and end Bytes
for(List<String> ls : fileMap.values()) {
if(ls.size()>1) {
while(ls.size()>=2){
int[] dupl = new int[ls.size()];
Arrays.fill(dupl, 0);
for (int j=0; j<(ls.size()-1); j++){
if(isEqualsFileFirstEndBytes(ls.get(j), ls.get(ls.size()-1))){
if(dupl[ls.size()-1]!=1) {
dupl[ls.size()-1]=1;
}
dupl[j]=1;
}
}
for(int j=0; j<=(dupl.length-1); j++){
if(dupl[j]==1){
if(!fileForCheck.contains(ls.get(j))) {
fileForCheck.add(ls.get(j));
duplicate.add(ls.get(j));
}
} else {
if(dupl.length>2 && j==(dupl.length-1)) list.add(ls.get(j));
if(dupl.length==2) list.add(ls.get(j));
}
}
if(duplicate.size()>0){
ls.removeAll(duplicate);
duplicate.clear();
} else if (ls.size()==2)
ls.clear();
else if (ls.size()>2)
ls.remove(ls.size()-1);
}
} else {
list.add(ls.get(0));
}
}
// 3 - Compare files by Bytes and remove the entry for duplicate
if(!fileForCheck.isEmpty()){
while(fileForCheck.size()>1) {
for (int j=0; j<(fileForCheck.size()-1); j++)
if(isEqualsFileByBytes(fileForCheck.get(j), fileForCheck.get(fileForCheck.size()-1))){
duplicate.add(fileForCheck.get(j));
if(j==(fileForCheck.size()-2)) {
list.add(fileForCheck.get(fileForCheck.size()-1));
mainFile.add(fileForCheck.get(fileForCheck.size()-1));
}
}
fileForCheck.removeAll(duplicate);
duplicate.clear();
fileForCheck.removeAll(mainFile);
mainFile.clear();
}
}
return list;
}
private Boolean isEqualsFileFirstEndBytes(String file1, String file2) {
File f1 = new File(file1);
File f2 = new File(file2);
byte[] b1 = new byte[100];
byte[] b2 = new byte[100];
byte[] b1end = new byte[100];
byte[] b2end = new byte[100];
if(f1.length()>200 && f2.length()>200) {
try ( RandomAccessFile randomAccessFile1 = new RandomAccessFile(file1, "r");
RandomAccessFile randomAccessFile2 = new RandomAccessFile(file2, "r")) {
randomAccessFile1.seek(0);
randomAccessFile2.seek(0);
randomAccessFile1.read(b1, 0, 100);
randomAccessFile2.read(b2, 0, 100);
randomAccessFile1.seek(new File(file1).length() - 100);
randomAccessFile2.seek(new File(file2).length() - 100);
randomAccessFile1.read(b1end, 0, 100);
randomAccessFile2.read(b2end, 0, 100);
if(Arrays.equals(b1,b2)==true && Arrays.equals(b1end,b2end)==true) return true;
else
return false;
} catch (IOException e) {
System.out.println("Exception is thrown : " + e);
e.printStackTrace();
}
}
return true;
}
private Boolean isEqualsFileByBytes(String file1, String file2) {
File f1 = new File(file1);
File f2 = new File(file2);
try(FileInputStream fis1 = new FileInputStream (f1);
FileInputStream fis2 = new FileInputStream (f2)) {
int n=0;
byte[] b1;
byte[] b2;
while ((n = fis1.available()) > 0) {
if (n>100) n=100;
b1 = new byte[n];
b2 = new byte[n];
int res1 = fis1.read(b1);
int res2 = fis2.read(b2);
if(res1!=-1 && res2!=-1){
if (Arrays.equals(b1,b2)==false) return false;
}
} // end while
}
catch (IOException e) {
System.out.println("Exception is thrown : " + e);
e.printStackTrace();
}
return true;
}
public List<String> listDuplicatingFiles(String dir) {
List<String> ldf = new ArrayList<>();
ldf = getFileNames(ldf, Paths.get(dir));
List<String> ldf2 = checkFiles(ldf);
return ldf2;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> fileNamesList = new ArrayList<>();
Problem2 prob2 = new Problem2();
fileNamesList = prob2.listDuplicatingFiles("c:\\File");
if(!fileNamesList.isEmpty()) System.out.println(fileNamesList);
}
}