public boolean isArchive(String input){
InputStream inFile = null;
//Initialise the value of 'valid' to false
boolean valid = false;
//This is the signature every file should have
int[] filesigrar = {0x58, 0x59, 0x12, 0xA2};
int[] filesigzip = {0x58, 0x59, 0x12, 0xA3};
try {
inFile = new FileInputStream(input);
//Go to the last 4 bytes
inFile.skip(input.length() - 4);
//Compare each of the last 4 bytes to the expected signatures
for(int i=0; i<4; i++) {
int c = inFile.read();
if(c == filesigrar[i]) {
valid = true;
} else if(c == filesigzip[i]) {
valid = true;
} else {
//In case any of the 4 bytes fails the check, the file is invalid
//and the loop is aborted.
valid = false;
break;
}
}
//Any exception assumes that the input is invalid.
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
valid = false;
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
valid = false;
}
System.out.println(valid);
return valid;
}