Example usage for java.io File equals

List of usage examples for java.io File equals

Introduction

In this page you can find the example usage for java.io File equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Tests this abstract pathname for equality with the given object.

Usage

From source file:org.kuali.kfs.sys.batch.BatchFileUtils.java

public static boolean isSuperDirectoryOf(File superDirectory, File directory) {
    superDirectory = superDirectory.getAbsoluteFile();

    while (directory != null) {
        directory = directory.getAbsoluteFile();
        if (directory.equals(superDirectory)) {
            return true;
        }//from  ww  w .  j  a v  a2 s. co m
        directory = directory.getParentFile();
    }
    return false;
}

From source file:arena.utils.FileUtils.java

/**
 * Used for checking path inheritances. This is useful for canonicalizing URLs that 
 * maybe don't exist, but we want to treat them as paths anyway.
 *///  ww w.  j  av  a  2  s. co  m
public static boolean isDescendant(File parent, File child, File commonBase) {
    if (child.equals(parent)) {
        return true;
    } else {
        // Start by checking canonicals if possible
        try {
            String canonicalParent = parent.getAbsoluteFile().getCanonicalPath();
            String canonicalChild = child.getAbsoluteFile().getCanonicalPath();
            if (canonicalChild.startsWith(canonicalParent)) {
                return true;
            }
        } catch (IOException err) {
            // assume the files are imaginary ... try with the manual construction below
        }

        // If canonicals don't match, we're dealing with symlinked files, so if we can
        // build a path from the parent to the child, 
        String childOCValue = constructOurCanonicalVersion(child, commonBase);
        String parentOCValue = constructOurCanonicalVersion(parent, commonBase);
        return childOCValue.startsWith(parentOCValue);
    }
}

From source file:au.org.ala.delta.util.FileUtils.java

/**
 * Attempts to make a relative path from path to file. If path and file have different roots (like drives under Windows), then
 * the path cannot be made relative, and in these cases this function will return null.
 * // w  ww.j  a  va 2 s. c om
 * @param path the source path
 * @param file the file/directory that will be made relative to the source path
 * @return either relative path from path to file, or null indicating that no common ancestor could be found (i.e. path cannot be made relative).
 * 
 */
public static String makeRelativeTo(String path, File file) {
    String relativePath;
    if (file.isAbsolute()) {
        File dataSetPath = new File(path);

        File parent = parent(file, dataSetPath);
        File commonParent = dataSetPath;
        String prefix = "";
        while (!parent.equals(commonParent) && commonParent != null) {
            prefix += ".." + File.separatorChar;
            commonParent = commonParent.getParentFile();
            parent = parent(file, commonParent);
        }

        if (commonParent == null) {
            // No common parent, cannot make relative
            return null;
        }

        String filePath = file.getAbsolutePath();
        String parentPath = parent.getAbsolutePath();

        int relativePathIndex = filePath.indexOf(parentPath) + parentPath.length();
        if (!parentPath.endsWith(File.separator)) {
            relativePathIndex++;
        }
        if (relativePathIndex > filePath.length()) {
            relativePathIndex = filePath.length();
        }
        relativePath = prefix + filePath.substring(relativePathIndex);
        if (StringUtils.isEmpty(relativePath)) {
            relativePath = ".";
        }
    } else {
        relativePath = file.getPath();
    }
    return relativePath;

}

From source file:mobac.program.EnvironmentSetup.java

/**
 * In case the <tt>mapsources</tt> directory has been moved by configuration (directories.ini or settings.xml) we
 * need to copy the existing map packs into the configured directory
 *//* w  ww.j a  v a2s  . c  o  m*/
public static void copyMapPacks() {
    File userMapSourcesDir = Settings.getInstance().getMapSourcesDirectory();
    File progMapSourcesDir = new File(DirectoryManager.programDir, "mapsources");
    if (userMapSourcesDir.equals(progMapSourcesDir))
        return; // no user specific directory configured
    if (userMapSourcesDir.isDirectory())
        return; // directory already exists - map packs should have been already copied
    try {
        Utilities.mkDirs(userMapSourcesDir);
        FileUtils.copyDirectory(progMapSourcesDir, userMapSourcesDir, new FileExtFilter(".jar"));
    } catch (IOException e) {
        log.error(e);
        JOptionPane.showMessageDialog(null, "Error on initializing mapsources directory:\n" + e.getMessage(),
                "Error", JOptionPane.ERROR_MESSAGE);
        System.exit(1);
    }
}

From source file:de.thischwa.pmcms.tool.file.FileTool.java

public static boolean isInside(File dir, File dirOrFile) {
    if (dir.equals(dirOrFile))
        return false;
    return (dirOrFile.getAbsolutePath().length() > dir.getAbsolutePath().length()
            && dirOrFile.getAbsolutePath().startsWith(dir.getAbsolutePath()));
}

From source file:org.datavyu.util.OFileUtils.java

/**
 * Calculate the difference in directory levels between basePath and path.
 * basePath must be a predecessor of path.
 * basePath must be a directory./*w ww. j a  va 2 s . c  o m*/
 * basePath and path must be valid paths of the same filesystem and mount
 * point.
 * basePath and path must be absolute paths.
 * Directories must have '/' at the end of the path.
 *
 * @param basePath
 * @param path
 * @return a positive integer >= 0 denoting the difference in directory
 *         levels if the difference can be determined. -1 if the difference
 *         cannot be determined.
 */
public static int levelDifference(final String basePath, final String path) {

    if ((basePath == null) || (path == null)) {
        throw new NullPointerException();
    }

    File base = new File(FilenameUtils.normalize(basePath, true));
    File ancestor = new File(FilenameUtils.getFullPath(FilenameUtils.normalize(path, true)));

    int diff = 0;

    while (!base.equals(ancestor)) {
        ancestor = ancestor.getParentFile();

        if (ancestor != null) {
            diff++;
        } else {
            return -1;
        }
    }

    return diff;
}

From source file:com.jaeksoft.searchlib.util.FileUtils.java

public final static boolean isSubDirectory(File base, File child) throws IOException {
    base = base.getCanonicalFile();//from  ww  w.ja  va 2s .  c  o  m
    child = child.getCanonicalFile();
    File parent = child;
    while (parent != null) {
        if (base.equals(parent))
            return true;
        parent = parent.getParentFile();
    }
    return false;
}

From source file:edu.unc.lib.dl.util.ZipFileUtil.java

private static boolean isFileInsideDirectory(File child, File parent) throws IOException {

    child = child.getCanonicalFile();//w  ww  .j  a  v a 2s.c  o  m
    parent = parent.getCanonicalFile();

    while (child != null) {
        child = child.getParentFile();

        if (parent.equals(child))
            return true;
    }

    return false;

}

From source file:org.flowerplatform.core.CoreUtils.java

private static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception {
    File folder = new File(srcFolder);
    if (folder.list().length == 0) {
        addFileToZip(path, srcFolder, zip, true);
    } else {//from  w  w  w . j a v  a 2  s. c o  m
        for (String fileName : folder.list()) {
            if (folder.equals(new File(CorePlugin.getInstance().getWorkspaceLocation()))
                    && CoreConstants.METADATA.equals(fileName)) {
                continue;
            }
            if (path.equals("")) {
                addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false);
            } else {
                addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false);
            }
        }
    }
}

From source file:Main.java

/** Returns true if these zip files act like equivalent sets.
 * The order of the zip entries is not important: if they contain
 * exactly the same contents, this returns true.
 * @param zip1 one zip file //from  w w  w.jav  a2  s  .  c  o m
 * @param zip2 another zip file
 * @return true if the two zip archives are equivalent sets
 * @throws IOException
 */
public static boolean zipEquals(File zip1, File zip2) throws IOException {
    if (zip1.equals(zip2))
        return true;

    InputStream in = null;
    ZipInputStream zipIn = null;
    try {
        in = new FileInputStream(zip1);
        zipIn = new ZipInputStream(in);
        ZipEntry e = zipIn.getNextEntry();
        Vector<String> entries = new Vector<String>();
        while (e != null) {
            entries.add(e.getName());

            InputStream other = getZipEntry(zip2, e.getName());
            if (other == null) {
                return false;
            }

            if (equals(zipIn, other) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }
        //now we've established everything in zip1 is in zip2

        //but what if zip2 has entries zip1 doesn't?
        zipIn.close();
        in.close();

        in = new FileInputStream(zip2);
        zipIn = new ZipInputStream(in);
        e = zipIn.getNextEntry();
        while (e != null) {
            if (entries.contains(e.getName()) == false) {
                return false;
            }
            e = zipIn.getNextEntry();
        }

        //the entries are exactly the same
        return true;
    } finally {
        try {
            zipIn.close();
        } catch (Throwable t) {
        }
        try {
            in.close();
        } catch (Throwable t) {
        }
    }
}