I am writing a backup program using Java and I would like to save the files and directories to the backup destination based on the absolute path names of the sources.
The backup program is supposed to work under different operating systems (Unix, Windows, Mac OS X), so I need to convert the absolute pathnames of the source files and directories.
So at the end I would like to convert an absolute source path to a regular sub-path of the destination. Let me give some examples.
Unix
Absolute source path: /home/thomas/data
Destination path: /backup
Effective destination: /backup/home/thomas/data
Windows
Absolute source path: c:\Documents and Settings\thomas
Destination path: e:\backup
Effective destination: e:\backup\c\Documents and Settings\thomas
I could use String replacement operations but I expect to run into several trouble and start a never ending correction story.
I think that Windows environment needs special handling because of the leading drive letters. Under *nix I could simply concatenate the two paths. Also note that the source path could be an UNC-path under Windows like \\share\data.
I have searched around and found several similar questions but none of them did answer mine (for example Is there a Java utility which will convert a String path to use the correct File separator char? or Convert Windows-style path into Unix path).
Maybe anybody has an idea or a hint for me or knows about a library that already does what I am seeking for.