All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dockerjava.core.util.FilePathUtil Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
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;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy