Package org.pgcodekeeper.core.utils
Class FileUtils
java.lang.Object
org.pgcodekeeper.core.utils.FileUtils
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 Summary
Modifier and TypeMethodDescriptionstatic PathcreateTempDirectory(Path dir, String prefix) Creates temporary directory in specified parent directory.static PathcreateTempFile(String prefix, String suffix) Creates temporary file with secure permissions.static PathcreateTempFile(Path dir, String prefix, String suffix) Creates temporary file in specified directory.static StringdbNameFromUrl(String url) Extracts database name from JDBC URL.static voidDeletes folder and its contents recursively.static StringGets current date and time formatted for filename usage.static PathgetLoadedFilePath(Path metaPath, URI uri) Generates unique file path for loaded content from URI.static StringgetNameFromUri(URI uri) Extracts name (last path segment) from URI.static PathgetUnzippedFilePath(Path metaPath, Path path) Generates unique file path for unzipped content based on original path.static StringgetValidFilename(String name) Gets valid filename by replacing invalid characters with underscores.static booleanChecks if file is a valid ZIP file by reading file signature.static voidDownloads content from URI to file in directory.static StringreadResource(Class<?> clazz, String fileName) Reads resource file content as UTF-8 string.static voidremoveReadOnly(Path path) Removes read-only attribute from file and deletes it.static StringsanitizeFilename(String name) Sanitizes filename by removing invalid characters.static StringExtracts ZIP file to target directory.
-
Method Details
-
deleteRecursive
Deletes folder and its contents recursively.- Parameters:
f- the path to delete recursively- Throws:
IOException- if deletion fails
-
removeReadOnly
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
Sanitizes filename by removing invalid characters. Removes characters:\ / : * ? " < > |- Parameters:
name- the filename to sanitize- Returns:
- sanitized filename with invalid characters removed
-
getValidFilename
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
Gets current date and time formatted for filename usage. Format: "yyyy-MM-dd HH''mm''ss"- Returns:
- formatted current date and time string
-
isZipFile
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
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 pathpath- the original file path- Returns:
- unique path for unzipped file
-
getLoadedFilePath
Generates unique file path for loaded content from URI. Creates name using valid filename + MD5 hash of URI path.- Parameters:
metaPath- the base metadata pathuri- the source URI- Returns:
- unique path for loaded file
-
dbNameFromUrl
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
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
Reads resource file content as UTF-8 string.- Parameters:
clazz- the class to load resource relative tofileName- the resource file name- Returns:
- resource content as UTF-8 string, or null if resource not found
- Throws:
IOException- if reading fails
-
loadURI
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 fromfileName- the target file namedir- the target directory- Throws:
IOException- if download or file operations fail
-
unzip
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 extractdir- the target directory for extraction- Returns:
- real path of extraction directory as string
- Throws:
IOException- if extraction fails
-
createTempFile
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 prefixsuffix- the file name suffix- Returns:
- path to created temporary file
- Throws:
IOException- if file creation fails
-
createTempFile
Creates temporary file in specified directory.- Parameters:
dir- the directory to create file inprefix- the file name prefixsuffix- the file name suffix- Returns:
- path to created temporary file
- Throws:
IOException- if file creation fails
-
createTempDirectory
Creates temporary directory in specified parent directory.- Parameters:
dir- the parent directoryprefix- the directory name prefix- Returns:
- path to created temporary directory
- Throws:
IOException- if directory creation fails
-