Came to this solution making use of JAVA 7+ AutoCloseable
with the try-with-resource pattern:
/**
* Check if a file is of a valid zip format.
*
* @param file the file to check
* @return true if file exists as file and is of a valid zip format
* @throws RuntimeException in case file open in general or auto closing the zip file did fail
*/
public static boolean isZipFile(File file) throws RuntimeException {
if ((file == null) || !file.exists() || !file.isFile()) {
return false;
}
// Use JAVA 7+ try-with-resource auto close pattern
try (ZipFile zipFile = new ZipFile(file)) {
// File can be read as valid zip file
return true;
} catch (ZipException zexc) {
// File is not of a valid zip format
return false;
} catch (IOException ioe) {
// Will be reached if opening file OR auto closing zipFile did fail
throw new RuntimeException("File " + file + " did fail to open or close", ioe);
}
}