Commit 7a80299d by Pedro Cavaleiro

Organized file handling functions into one class

Helpers.FileHelpers
parent b4bd2191
...@@ -25,7 +25,7 @@ public class FallIntoOblivion { ...@@ -25,7 +25,7 @@ public class FallIntoOblivion {
private static Configs conf = new Configs(); private static Configs conf = new Configs();
private static ReentrantLock propertiesLock = new ReentrantLock(); private static ReentrantLock propertiesLock = new ReentrantLock();
private static final String availableCommands = "Available commands: setcypher setvalidation sethash setenabled showconfig\n"; private static final String availableCommands = "Available commands: setcypher sethash setenabled showconfig\n";
private static final String setCypherCommandString = "setcypher [cyphertype] [keysize]\nCypher Types:\naes_cbc\n"; private static final String setCypherCommandString = "setcypher [cyphertype] [keysize]\nCypher Types:\naes_cbc\n";
private static final String setFileValidationCommandString = "setvalidation [validationmethod]\nAvailable Methods:\nhash\ndigital_signature\n"; private static final String setFileValidationCommandString = "setvalidation [validationmethod]\nAvailable Methods:\nhash\ndigital_signature\n";
...@@ -63,15 +63,15 @@ public class FallIntoOblivion { ...@@ -63,15 +63,15 @@ public class FallIntoOblivion {
try{ try{
Dir.mkdir(); Dir.mkdir();
result = true; result = true;
} }
catch(SecurityException se){ catch(SecurityException se){
System.out.println("Something went wrong with creating your new very useless trash folder"); System.out.println("Something went wrong with creating your new very useless trash folder");
} }
if(result) { if(result) {
System.out.println("DIR created"); System.out.println("DIR created");
} }
} }
//Test if FallIntoOblivion has a trashed folder if not create it //Test if FallIntoOblivion has a trashed folder if not create it
File TrashDir = new File(Dir.getName() + "/Trashed"); File TrashDir = new File(Dir.getName() + "/Trashed");
if (!TrashDir.exists()) { if (!TrashDir.exists()) {
...@@ -80,11 +80,11 @@ public class FallIntoOblivion { ...@@ -80,11 +80,11 @@ public class FallIntoOblivion {
try{ try{
TrashDir.mkdir(); TrashDir.mkdir();
result = true; result = true;
} }
catch(SecurityException se){ catch(SecurityException se){
System.out.println("You shouln't delete your Trashed folder"); System.out.println("You shouln't delete your Trashed folder");
} }
if(result) { if(result) {
System.out.println("Trashed DIR created"); System.out.println("Trashed DIR created");
} }
} }
...@@ -143,12 +143,12 @@ public class FallIntoOblivion { ...@@ -143,12 +143,12 @@ public class FallIntoOblivion {
} catch (Exception ex) { } catch (Exception ex) {
System.out.println(ex); System.out.println(ex);
} }
deleteDirectory(file); Helpers.FileHelpers.deleteDirectory(file);
WatchDir.foldersToEncrypt.remove(file); WatchDir.foldersToEncrypt.remove(file);
file = new File(file.getAbsolutePath() + ".zip"); file = new File(file.getAbsolutePath() + ".zip");
} }
byte[] fBytes = Ler.umFicheiro(file.getAbsolutePath()); byte[] fBytes = Helpers.FileHelpers.umFicheiro(file.getAbsolutePath());
File folder = new File("Fall_Into_Oblivion/Trashed/" + file.getName()); File folder = new File("Fall_Into_Oblivion/Trashed/" + file.getName());
if (!folder.exists()) { if (!folder.exists()) {
...@@ -162,7 +162,7 @@ public class FallIntoOblivion { ...@@ -162,7 +162,7 @@ public class FallIntoOblivion {
String zeroHASH = SHA256.calculateStringMAC("0000"); String zeroHASH = SHA256.calculateStringMAC("0000");
System.out.println(zeroHASH.subSequence(0, 16).toString()); System.out.println(zeroHASH.subSequence(0, 16).toString());
byte[] encBytes = AES_CBC.encrypt(zeroHASH.subSequence(0, 16).toString(), "0000000000000000", fBytes); byte[] encBytes = AES_CBC.encrypt(zeroHASH.subSequence(0, 16).toString(), "0000000000000000", fBytes);
Ler.escreverFicheiro("Fall_Into_Oblivion/Trashed/" + file.getName() + "/" + file.getName(), cyphertype.replaceAll("_",""), encBytes); Helpers.FileHelpers.escreverFicheiro("Fall_Into_Oblivion/Trashed/" + file.getName() + "/" + file.getName(), cyphertype.replaceAll("_",""), encBytes);
encryptedFolders.add(folder.toString()); encryptedFolders.add(folder.toString());
file.delete(); file.delete();
WatchDir.foldersToEncrypt.remove(f); WatchDir.foldersToEncrypt.remove(f);
...@@ -305,6 +305,7 @@ public class FallIntoOblivion { ...@@ -305,6 +305,7 @@ public class FallIntoOblivion {
/** /**
* Define qual o método pelo qual vai ser verificada a integridade dos ficheiros * Define qual o método pelo qual vai ser verificada a integridade dos ficheiros
* Disponiveis atualmente HASH e Assinatura Digital * Disponiveis atualmente HASH e Assinatura Digital
* Atualmente só será usada Assinatura digital
* @param words um array de strings que contem os comandos necessários * @param words um array de strings que contem os comandos necessários
*/ */
private static void setFileValidationMethod(String[] words) { private static void setFileValidationMethod(String[] words) {
...@@ -364,6 +365,7 @@ public class FallIntoOblivion { ...@@ -364,6 +365,7 @@ public class FallIntoOblivion {
System.out.println("setEnabled [boolean]\n"); //add new types here too System.out.println("setEnabled [boolean]\n"); //add new types here too
} }
} }
public static String listToString(ArrayList<String> list) { public static String listToString(ArrayList<String> list) {
String result = ""; String result = "";
...@@ -375,21 +377,4 @@ public class FallIntoOblivion { ...@@ -375,21 +377,4 @@ public class FallIntoOblivion {
} }
return result; return result;
} }
public static boolean deleteDirectory(File directory) {
if(directory.exists()){
File[] files = directory.listFiles();
if(null!=files){
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
}
return(directory.delete());
}
} }
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
*/ */
package fallintooblivion; package fallintooblivion;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/** /**
* *
* @author pedrocavaleiro * @author pedrocavaleiro
...@@ -12,7 +16,58 @@ package fallintooblivion; ...@@ -12,7 +16,58 @@ package fallintooblivion;
public class Helpers { public class Helpers {
public static class FileHelpers { public static class FileHelpers {
/**
* Apaga um diretório recursivamente
* @param directory diretório a ser apagado
* @return verdadeiro se foi apagado caso contrario falso
*/
public static boolean deleteDirectory(File directory) {
if(directory.exists()){
File[] files = directory.listFiles();
if(null!=files){
for(int i=0; i<files.length; i++) {
if(files[i].isDirectory()) {
deleteDirectory(files[i]);
}
else {
files[i].delete();
}
}
}
}
return(directory.delete());
}
/**
* Lê um ficheiro inteiro para a memoria. Não recomendado para ficheiros
* grandes
* @param filePath caminho completo para o ficheiro
* @return devolve os bytes do ficheiro
* @throws Exception
*/
public static byte[] umFicheiro(String filePath) throws Exception{
File file = new File(filePath);
byte[] getBytes = new byte[(int) file.length()];
FileInputStream fsIn = new FileInputStream(file);
fsIn.read(getBytes);
fsIn.close();
return getBytes;
}
/**
* Escreve um ficheiro para o disco.
* @param filepath Ficheiro onde serão escritos os bytes
* @param extension Extensão do ficheiro
* @param bytes Bytes para serem escritos
* @throws Exception Excepção generica
*/
public static void escreverFicheiro(String filepath, String extension, byte[] bytes) throws Exception {
FileOutputStream fsOut = new FileOutputStream(filepath + "." + extension);
fsOut.write(bytes);
fsOut.close();
}
} }
} }
...@@ -50,27 +50,4 @@ public class Ler{ ...@@ -50,27 +50,4 @@ public class Ler{
} }
} }
} }
/**
* Lê um ficheiro inteiro para a memoria. Não recomendado para ficheiros
* grandes
* @param filePath caminho completo para o ficheiro
* @return devolve os bytes do ficheiro
* @throws Exception
*/
public static byte[] umFicheiro(String filePath) throws Exception{
File file = new File(filePath);
byte[] getBytes = new byte[(int) file.length()];
FileInputStream fsIn = new FileInputStream(file);
fsIn.read(getBytes);
fsIn.close();
return getBytes;
}
public static void escreverFicheiro(String filepath, String extension, byte[] bytes) throws Exception {
FileOutputStream fsOut = new FileOutputStream(filepath + "." + extension);
fsOut.write(bytes);
fsOut.close();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment