-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUtils.java
More file actions
206 lines (182 loc) · 5.94 KB
/
Utils.java
File metadata and controls
206 lines (182 loc) · 5.94 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
import java.io.*;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class Utils {
///
/// READS FILE FROM PATH, RETURNS STRING
///
public static String readFile(String path) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(path));
String content = "";
// store all lines of target file
while (reader.ready()) {
content += reader.readLine() + "\n";
}
reader.close();
return content;
}
///
/// MAKES FILE BASED ON PATH
/// RETURNS FALSE IF ALREADY EXISTS
///
public static boolean makeFile(String path) throws IOException {
// create new file, if it doesn't already exist
File newFile = new File(path);
if (!newFile.exists())
newFile.createNewFile();
else
return false;
return true;
}
///
/// MAKES FILE BASED ON PATH, THEN WRITES CONTENT TO IT
/// RETURNS FALSE IF ALREADY EXISTS
///
public static boolean makeFile(String path, String content) throws IOException {
// create new , if it doesn't already exist
File newFile = new File(path);
if (!newFile.exists())
newFile.createNewFile();
else
return false;
// write content into the new file
BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
writer.write(content);
writer.close();
return true;
}
///
/// WRITES CONTENT INTO FILE/PATH
///
public static void write(String path, String content) throws IOException {
// write all content into the file
BufferedWriter writer = new BufferedWriter(new FileWriter(path, true));
writer.write(content);
writer.close();
}
///
/// MAKES DIRECTORY INTO BASED ON PATH
/// RETURNS FALSE IF ALREADY EXISTS
///
public static boolean makeDir(String path) throws IOException {
// make a new directory, if it doesn't already exist
File newDir = new File(path);
if (!newDir.exists())
newDir.mkdir();
else
return false;
return true;
}
///
/// DELETES THE FILE AT THE GIVEN PATH
/// RETURNS FALSE IF FILE NOT FOUND
///
public static boolean deleteFile(String path) throws IOException {
File file = new File(path);
if (!file.exists())
return false;
file.delete();
return true;
}
///
/// DELETES THE GIVEN FILE
/// RETURNS FALSE IF FILE NOT FOUND
///
public static boolean deleteFile(File file) throws IOException {
if (!file.exists())
return false;
file.delete();
return true;
}
///
/// DELETES ALL FILES IN AND INCLUDING GIVEN DIRECTORY AT PATH
/// RETURNS FALSE IF DIRECTORY NOT FOUND
///
public static boolean deleteDirectory(String path) throws IOException {
File file = new File(path);
if (!file.exists())
return false;
File[] allFiles = file.listFiles();
for (File f : allFiles) {
deleteDirectory(f);
}
return file.delete();
}
///
/// DELETES ALL FILES IN AND INCLUDING GIVEN FILE
/// RETURNS FALSE IF FILE NOT FOUND
///
public static boolean deleteDirectory(File file) throws IOException {
File[] allFiles = file.listFiles();
if (allFiles != null) {
for (File f : allFiles) {
deleteDirectory(f);
}
}
return file.delete();
}
///
/// RETURNS THE SHA1 OF THE STRING
///
public static String SHA1(String input) {
try {
// getInstance() method is called with algorithm SHA-1
MessageDigest md = MessageDigest.getInstance("SHA-1");
// digest() method is called
// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = md.digest(input.getBytes());
// Convert byte array into signum representation
BigInteger no = new BigInteger(1, messageDigest);
// Convert message digest into hex value
String hashtext = no.toString(16);
// Add preceding 0s to make it 40 digits long
while (hashtext.length() < 40) {
hashtext = "0" + hashtext;
}
// return the HashText
return hashtext;
}
// For specifying wrong message digest algorithms
catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
///
/// COMPRESSES THE INPUT AND RETURNS IT
///
public static byte[] compress(String str) throws IOException {
int size = 1024;
BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(str.getBytes()));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos);
byte[] buffer = new byte[size];
int len;
while ((len = bis.read(buffer, 0, size)) != -1) {
gzip.write(buffer, 0, len);
}
gzip.finish();
bis.close();
gzip.close();
return baos.toByteArray();
}
///
/// DECOMPRESSES THE INPUT AND RETURNS IT
///
public static String uncompress(byte[] data) throws IOException {
int size = 1024;
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(data));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[size];
int len;
while ((len = gzip.read(buffer, 0, size)) != -1) {
baos.write(buffer, 0, len);
}
gzip.close();
baos.close();
return new String(baos.toByteArray());
}
}