Class FileUtils

java.lang.Object
org.pgcodekeeper.core.utils.FileUtils

public final class FileUtils extends Object
Comprehensive file system utility class providing various file operations, temporary file/directory management, ZIP file handling, filename validation, and database project-related file operations.
  • Method Details

    • deleteRecursive

      public static void deleteRecursive(Path f) throws IOException
      Deletes folder and its contents recursively.
      Parameters:
      f - the path to delete recursively
      Throws:
      IOException - if deletion fails
    • removeReadOnly

      public static void removeReadOnly(Path path) throws IOException
      Removes read-only attribute from file and deletes it. Handles DOS file attributes and symlink edge cases on Linux.
      Parameters:
      path - the path to remove read-only attribute from and delete
      Throws:
      IOException - if operation fails
    • sanitizeFilename

      public static String sanitizeFilename(String name)
      Sanitizes filename by removing invalid characters. Removes characters: \ / : * ? " < > |
      Parameters:
      name - the filename to sanitize
      Returns:
      sanitized filename with invalid characters removed
    • getValidFilename

      public static String getValidFilename(String name)
      Gets valid filename by replacing invalid characters with underscores. Replaces characters: \ / : * ? " < > | with '_'
      Parameters:
      name - the filename to validate
      Returns:
      valid filename with invalid characters replaced by underscores
    • getFileDate

      public static String getFileDate()
      Gets current date and time formatted for filename usage. Format: "yyyy-MM-dd HH''mm''ss"
      Returns:
      formatted current date and time string
    • isZipFile

      public static boolean isZipFile(Path path) throws IOException
      Checks if file is a valid ZIP file by reading file signature. Recognizes ZIP file signatures: 0x504B0304, 0x504B0506, 0x504B0708
      Parameters:
      path - the file path to check
      Returns:
      true if file is a valid ZIP file, false otherwise
      Throws:
      IOException - if file cannot be read
    • getUnzippedFilePath

      public static Path getUnzippedFilePath(Path metaPath, Path path)
      Generates unique file path for unzipped content based on original path. Creates name using original filename + MD5 hash of relative path.
      Parameters:
      metaPath - the base metadata path
      path - the original file path
      Returns:
      unique path for unzipped file
    • getLoadedFilePath

      public static Path getLoadedFilePath(Path metaPath, URI uri)
      Generates unique file path for loaded content from URI. Creates name using valid filename + MD5 hash of URI path.
      Parameters:
      metaPath - the base metadata path
      uri - the source URI
      Returns:
      unique path for loaded file
    • dbNameFromUrl

      public static String dbNameFromUrl(String url)
      Extracts database name from JDBC URL. Handles Microsoft SQL Server URLs specially, otherwise uses URI parsing.
      Parameters:
      url - the JDBC URL
      Returns:
      database name extracted from URL, or empty string if extraction fails
    • getNameFromUri

      public static String getNameFromUri(URI uri)
      Extracts name (last path segment) from URI.
      Parameters:
      uri - the URI to extract name from
      Returns:
      last path segment of URI, or full URI string if path is null, or null if URI is null
    • readResource

      public static String readResource(Class<?> clazz, String fileName) throws IOException
      Reads resource file content as UTF-8 string.
      Parameters:
      clazz - the class to load resource relative to
      fileName - the resource file name
      Returns:
      resource content as UTF-8 string, or null if resource not found
      Throws:
      IOException - if reading fails
    • loadURI

      public static void loadURI(URI uri, String fileName, Path dir) throws IOException
      Downloads content from URI to file in directory. Creates directory if it doesn't exist. Handles race conditions and cleanup on failure.
      Parameters:
      uri - the URI to download from
      fileName - the target file name
      dir - the target directory
      Throws:
      IOException - if download or file operations fail
    • unzip

      public static String unzip(Path zip, Path dir) throws IOException
      Extracts ZIP file to target directory. Uses temporary directory with unique name to avoid parallel extraction conflicts. Returns real path of extraction directory.
      Parameters:
      zip - the ZIP file to extract
      dir - the target directory for extraction
      Returns:
      real path of extraction directory as string
      Throws:
      IOException - if extraction fails
    • createTempFile

      public static Path createTempFile(String prefix, String suffix) throws IOException
      Creates temporary file with secure permissions. On POSIX systems, sets permissions to rwx------. On other systems, uses file attribute methods.
      Parameters:
      prefix - the file name prefix
      suffix - the file name suffix
      Returns:
      path to created temporary file
      Throws:
      IOException - if file creation fails
    • createTempFile

      public static Path createTempFile(Path dir, String prefix, String suffix) throws IOException
      Creates temporary file in specified directory.
      Parameters:
      dir - the directory to create file in
      prefix - the file name prefix
      suffix - the file name suffix
      Returns:
      path to created temporary file
      Throws:
      IOException - if file creation fails
    • createTempDirectory

      public static Path createTempDirectory(Path dir, String prefix) throws IOException
      Creates temporary directory in specified parent directory.
      Parameters:
      dir - the parent directory
      prefix - the directory name prefix
      Returns:
      path to created temporary directory
      Throws:
      IOException - if directory creation fails