-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManager.java
91 lines (82 loc) · 2.37 KB
/
FileManager.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
package cryptography;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/*
* Encapsulates the file helper methods for crypto package.
* @Richard Kavanagh
*/
public class FileManager {
/*
* Reads in a zip file from the local directory and stores as a byte array.
*/
public byte [] readZipFileBytes(String fileName) throws IOException {
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(fileName));
StringBuilder zipContents = new StringBuilder();
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String entryName = zipEntry.getName();
File newFile = new File(entryName);
String directory = newFile.getParent();
if (directory == null) {
if (newFile.isDirectory())
break;
}
zipContents.append(readIndividualFileBytes(newFile));
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.close();
return zipContents.toString().getBytes("UTF-8");
}
/*
* Reads in a file within a (.zip) file.
*/
private String readIndividualFileBytes(File entryName) {
RandomAccessFile randomFile;
StringBuilder fileContents = new StringBuilder();
try {
randomFile = new RandomAccessFile(entryName, "r");
String line;
while ((line = randomFile.readLine()) != null) {
fileContents.append(line + "\n");
}
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
return fileContents.toString();
}
/*
* Reads in a file from the local directory and stores as a byte array.
*/
public byte [] readTextFileBytes(String name) {
byte[] fileContent = null;
try {
Path path = FileSystems.getDefault().getPath(".", name);
return Files.readAllBytes(path);
}
catch (IOException e) {
e.printStackTrace();
}
return fileContent;
}
/*
* Writes a string to a file in the local directory.
*/
public void writeToFile(String prefix, String content, String fileName) {
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)))) {
out.println(prefix + " : " + content);
}catch (IOException e) {
e.printStackTrace();
}
}
}