79556765

Date: 2025-04-05 09:23:18
Score: 0.5
Natty:
Report link

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);
    }
}
Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Markus Hoffrogge