-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectory.java
266 lines (238 loc) · 8.88 KB
/
Directory.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
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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Directory info. Hold the files that it has and tracks statiscs. To know more
* info: https://github.com/FlyingWolFox/Duplicate-Finder
*
* @version 1.3
* @author FlyingWolFox
*/
public class Directory {
private ArrayList<FileInfo> files;
private ArrayList<Archive> archives;
private Path path;
private int num; // dir id
private static int x; // used to generate dir id
private int numOfInternalRepetions;
private int numOfRepetions;
static {
x = 0;
}
/**
* Main contructor, takes the path to the directory and process all files
*
* @param path path to the directory
* @throws IOException if there was an error opening the directory
* @Deprecated
* @see Directory(String path)
*/
public Directory(String path, String deprecated) throws IOException {
num = x; // gets dir id
this.path = Paths.get(path);
files = new ArrayList<FileInfo>();
archives = new ArrayList<Archive>();
System.out.println("");
System.out.println("Opening " + path);
// open the DirectoryStream to get files
DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(path));
for (Path file : stream) {
if (!file.toFile().isDirectory()) {
if (Archive.isArchive(file.getFileName().toString())) {
archives.add(new Archive(file.toFile(), this));
} else
files.add(new FileInfo(file.toFile(), this));
}
}
trasnformSingleFileArchives();
Collections.sort(files); // sorts based on hash
if (archives.size() != 0)
Collections.sort(archives, archives.get(0).new ArchiveComparator());
x++;
stream.close();
}
public Directory(String path) throws IOException {
num = x; // gets dir id
this.path = Paths.get(path);
files = new ArrayList<FileInfo>();
archives = new ArrayList<Archive>();
FileInfo[][] cache = Cache.getCache(this);
List<FileInfo> fileCache = new ArrayList<FileInfo>(Arrays.asList(cache[0]));
List<Archive> archiveCache = new ArrayList<Archive>(Arrays.asList((Archive[]) cache[1]));
ArrayList<FileInfo> fileDeletions = new ArrayList<FileInfo>();
ArrayList<FileInfo> fileAditions = new ArrayList<FileInfo>();
ArrayList<Archive> archiveDeletions = new ArrayList<Archive>();
ArrayList<Archive> archiveAditions = new ArrayList<Archive>();
// Even if the cache file doesn't exists, nothing should break
for (FileInfo file : fileCache) {
if (!file.getFile().exists())
fileDeletions.add(file);
else if (!String.valueOf(file.getFile().lastModified()).equals(file.getLastModified())) {
fileDeletions.add(file);
fileAditions.add(file);
}
}
ProgressBar bar = new ProgressBar("Hashing old file cache entries", fileAditions.size());
fileCache.removeAll(fileDeletions);
for (int i = 0; i < fileAditions.size(); i++) {
fileAditions.set(i, new FileInfo(fileAditions.get(i).getFile(), this));
bar.update();
}
fileCache.addAll(fileAditions);
for (Archive archive : archiveCache) {
if (!archive.getFile().exists())
archiveDeletions.add(archive);
else if (!String.valueOf(archive.getFile().lastModified()).equals(archive.getLastModified())) {
archiveDeletions.add(archive);
archiveAditions.add(archive);
}
}
bar = new ProgressBar("Hashing old archive cache entries", archiveAditions.size());
archiveCache.removeAll(archiveDeletions);
for (int i = 0; i < archiveAditions.size(); i++) {
archiveAditions.set(i, new Archive(archiveAditions.get(i).getFile(), this));
bar.update();
}
archiveCache.addAll(archiveAditions);
ArrayList<Path> filesPaths = new ArrayList<Path>();
for (FileInfo file : fileCache)
filesPaths.add(file.getPath());
ArrayList<Path> archivesPaths = new ArrayList<Path>();
for (Archive archive : archiveCache)
archivesPaths.add(archive.getPath());
// open the DirectoryStream to get files
DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(path));
for (Path file : stream) {
if (!file.toFile().isDirectory()) {
if (Archive.isArchive(file.getFileName().toString())) {
archivesPaths.add(file);
} else
filesPaths.add(file);
}
}
stream.close();
Collections.sort(filesPaths);
Collections.sort(archivesPaths);
for (int i = 0; i < filesPaths.size() - 1; i++) {
String name1 = filesPaths.get(i).getFileName().toString();
String name2 = filesPaths.get(i + 1).getFileName().toString();
if (name1.equals(name2)) {
filesPaths.remove(i + 1);
filesPaths.remove(i);
i--;
}
}
for (int i = 0; i < archivesPaths.size() - 1; i++) {
String name1 = archivesPaths.get(i).getFileName().toString();
String name2 = archivesPaths.get(i + 1).getFileName().toString();
if (name1.equals(name2)) {
archivesPaths.remove(i + 1);
archivesPaths.remove(i);
i--;
}
}
System.out.println("");
System.out.println("Opening " + path);
bar = new ProgressBar("Hashing Files", filesPaths.size());
for (Path file : filesPaths) {
fileAditions.add(new FileInfo(file.toFile(), this));
bar.update();
}
bar = new ProgressBar("Hashing Archives", archivesPaths.size());
for (Path archive : archivesPaths) {
archiveAditions.add(new Archive(archive.toFile(), this));
bar.update();
}
files.addAll(fileCache);
files.addAll(fileAditions);
archives.addAll(archiveCache);
archives.addAll(archiveAditions);
FileInfo[][] filesUpdate = { fileAditions.toArray(new FileInfo[fileAditions.size()]),
fileDeletions.toArray(new FileInfo[fileDeletions.size()]) };
Archive[][] archivesUpdate = { archiveAditions.toArray(new Archive[archiveAditions.size()]),
archiveDeletions.toArray(new Archive[archiveDeletions.size()]) };
Cache.updateCache(this, filesUpdate, archivesUpdate);
trasnformSingleFileArchives();
Collections.sort(files); // sorts based on hash
if (archives.size() != 0)
Collections.sort(archives, archives.get(0).new ArchiveComparator());
x++;
}
/**
* looks for archives that contains a single file. Transforms archive into file
* by using the compressed file hash instead of the archive file hash and puts
* it with in files ArrayList
*/
public void trasnformSingleFileArchives() {
// Uses iterator to avoid java.util.ConcurrentModificationException
for (Iterator<Archive> i = archives.iterator(); i.hasNext();) {
Archive archive = i.next();
if (archive.hasSingleFile()) {
archive.getHashFromFile();
files.add(archive);
i.remove();
}
}
}
/**
* @return the path to this dir
*/
public Path getPath() {
return path;
}
/**
* @return the ArrayList of files of this dir
*/
public ArrayList<FileInfo> getFiles() {
return files;
}
/**
* @return ArrayList of archives of this dir
*/
public ArrayList<Archive> getArchives() {
return archives;
}
/**
* @return the id of this dir
*/
public int getNum() {
return num;
}
/**
* @return the number of directories already created
*/
public static int getX() {
return x;
}
/**
* @return the number of internal repetions
*/
public int getNumOfInternalRepetions() {
return numOfInternalRepetions;
}
/**
* Increases the number of internal repetions by one
*/
public void increaseNumOfInternalRepetions() {
this.numOfInternalRepetions++;
}
/**
* @return the number of external repetions
*/
public int getNumOfRepetions() {
return numOfRepetions;
}
/**
* Increases the number of external repetions by one
*/
public void increaseNumOfRepetions() {
this.numOfRepetions++;
}
}