Package org.kendar.sync.lib.utils
Class FileUtils
java.lang.Object
org.kendar.sync.lib.utils.FileUtils
Utility class for file operations.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncalculateFileDifferences(List<FileInfo> sourceFiles, List<FileInfo> targetFiles, BackupType backupType) Calculates the files that need to be transferred and deleted.static voidCopies a file.static voidcreateDirectoryIfNotExists(File directory) Creates a directory if it doesn't exist.static voidDeletes a file or directory.static booleandeleteDirectoryContents(Path directory) Deletes all files and subdirectories in the specified directory.static StringgetTargetPath(FileInfo file, String targetDir, BackupType backupType) Gets the target path for a file based on the backup type.Lists all files in a directory recursively.static StringmakeUniformPath(String path) static booleanChecks if a given path matches a specified pattern.static byte[]Reads a file into a byte array.static Stringstatic Stringstatic AttributesreadFileAttributes(Path path) static voidsetFileTimes(File file, Instant creationTime, Instant modificationTime) Sets the file times.static voidsetFileTimesToEpoch(File file) Sets the file times to epoch (1970-01-01 00:00:00).static voidWrites a byte array to a file.static voidwriteFileAttributes(Path path, int umask, BasicFileAttributes attributes) Converts a Unix file mode (umask) to a set of PosixFilePermission
-
Constructor Details
-
FileUtils
public FileUtils()
-
-
Method Details
-
readFile
- Throws:
IOException
-
readFile
- Throws:
IOException
-
listFiles
Lists all files in a directory recursively.- Parameters:
directory- The directory to listbaseDir- The base directory for calculating relative paths- Returns:
- A list of file information
- Throws:
IOException- If an I/O error occurs
-
makeUniformPath
-
calculateFileDifferences
public static Map<String,List<FileInfo>> calculateFileDifferences(List<FileInfo> sourceFiles, List<FileInfo> targetFiles, BackupType backupType) Calculates the files that need to be transferred and deleted.- Parameters:
sourceFiles- The source filestargetFiles- The target filesbackupType- The backup type- Returns:
- A map with the files to transfer and delete
-
getTargetPath
Gets the target path for a file based on the backup type.- Parameters:
file- The filetargetDir- The target directorybackupType- The backup type- Returns:
- The target path
-
createDirectoryIfNotExists
Creates a directory if it doesn't exist.- Parameters:
directory- The directory to create- Throws:
IOException- If an I/O error occurs
-
setFileTimes
public static void setFileTimes(File file, Instant creationTime, Instant modificationTime) throws IOException Sets the file times.- Parameters:
file- The filecreationTime- The creation timemodificationTime- The modification time- Throws:
IOException- If an I/O error occurs
-
setFileTimesToEpoch
Sets the file times to epoch (1970-01-01 00:00:00).- Parameters:
file- The file- Throws:
IOException- If an I/O error occurs
-
delete
Deletes a file or directory.- Parameters:
file- The file or directory to delete- Throws:
IOException- If an I/O error occurs
-
copyFile
Copies a file.- Parameters:
source- The source filetarget- The target file- Throws:
IOException- If an I/O error occurs
-
readFile
Reads a file into a byte array.- Parameters:
file- The file to read- Returns:
- The file contents as a byte array
- Throws:
IOException- If an I/O error occurs
-
writeFile
Writes a byte array to a file.- Parameters:
file- The file to write todata- The data to write- Throws:
IOException- If an I/O error occurs
-
deleteDirectoryContents
Deletes all files and subdirectories in the specified directory. The directory itself is not deleted.- Parameters:
directory- The directory to clean- Returns:
- true if all files were deleted successfully, false otherwise
- Throws:
IOException- If an I/O error occurs
-
matches
Checks if a given path matches a specified pattern. The pattern can be a glob pattern or a regex pattern (prefixed with '@').- Parameters:
path- The path to checkpattern- The pattern to match against- Returns:
- true if the path matches the pattern, false otherwise
-
readFileAttributes
- Throws:
IOException
-
writeFileAttributes
public static void writeFileAttributes(Path path, int umask, BasicFileAttributes attributes) throws IOException Converts a Unix file mode (umask) to a set of PosixFilePermission- Parameters:
umask- The Unix file mode as an integer (e.g., 644, 755)- Throws:
IOException
-