forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePathUtil.java
More file actions
52 lines (46 loc) · 1.52 KB
/
Copy pathFilePathUtil.java
File metadata and controls
52 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.github.dockerjava.core.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import com.github.dockerjava.api.exception.DockerClientException;
public class FilePathUtil {
private FilePathUtil() {
// utility class
}
/**
* Return the relative path. Path elements are separated with / char.
*
* @param baseDir
* a parent directory of {@code file}
* @param file
* the file to get the relative path
* @return the relative path
*/
public static String relativize(File baseDir, File file) {
try {
baseDir = baseDir.getCanonicalFile();
file = file.getCanonicalFile();
return baseDir.toURI().relativize(file.toURI()).getPath();
} catch (IOException e) {
throw new DockerClientException(e.getMessage(), e);
}
}
/**
* Return the relative path. Path elements are separated with / char.
*
* @param baseDir
* a parent directory of {@code file}
* @param file
* the file to get the relative path
* @return the relative path
*/
public static String relativize(Path baseDir, Path file) {
String path = baseDir.toUri().relativize(file.toUri()).getPath();
if (!"/".equals(baseDir.getFileSystem().getSeparator())) {
// For windows
return path.replace(baseDir.getFileSystem().getSeparator(), "/");
} else {
return path;
}
}
}