public File getCommonDir ( final File dir1 , final File dir2 ) throws IOException { List < File > parts1 = getParentDirs ( dir1 ) ; List < File > parts2 = getParentDirs ( dir2 ) ; File matched = null ; final int maxCommonSize = Math . min ( parts1 . size ( ) , parts2 . size ( ) ) ; for ( int i = 0 ; i < maxCommonSize ; ++ i ) { if ( parts1 . get ( i ) . equals ( parts2 . get ( i ) ) ) { matched = parts1 . get ( i ) ; } else { break ; } } return matched ; }
Gets the longest common parent directory path of two paths .