@Test public void testIsAncestorFile() throws Exception { File parent = new File( "home/toto/whatever" ); Assert.assertTrue( Utils.isAncestorFile( parent, parent )); File comp = new File( "home/toto/whatever/" ); Assert.assertTrue( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/./whatever/" ); Assert.assertTrue( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/../toto/whatever/" ); Assert.assertTrue( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/whatever/some-file.txt" ); Assert.assertTrue( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/whatever/some/dir/some-file.txt" ); Assert.assertTrue( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/" ); Assert.assertFalse( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/whateve" ); Assert.assertFalse( Utils.isAncestorFile( parent, comp )); comp = new File( "home/toto/whatevereeeeeee" ); Assert.assertFalse( Utils.isAncestorFile( parent, comp )); }
public static boolean isAncestorFile( File potentialAncestor, File file ) throws IOException { String ancestorPath = potentialAncestor.getCanonicalPath(); String path = file.getCanonicalPath(); boolean result = false; if( path.startsWith( ancestorPath )) { String s = path.substring( ancestorPath.length()); result = s.isEmpty() || s.startsWith( System.getProperty( "file.separator" )); } return result; }
Utils { public static boolean isAncestorFile( File potentialAncestor, File file ) throws IOException { String ancestorPath = potentialAncestor.getCanonicalPath(); String path = file.getCanonicalPath(); boolean result = false; if( path.startsWith( ancestorPath )) { String s = path.substring( ancestorPath.length()); result = s.isEmpty() || s.startsWith( System.getProperty( "file.separator" )); } return result; } }
Utils { public static boolean isAncestorFile( File potentialAncestor, File file ) throws IOException { String ancestorPath = potentialAncestor.getCanonicalPath(); String path = file.getCanonicalPath(); boolean result = false; if( path.startsWith( ancestorPath )) { String s = path.substring( ancestorPath.length()); result = s.isEmpty() || s.startsWith( System.getProperty( "file.separator" )); } return result; } private  Utils(); }
Utils { public static boolean isAncestorFile( File potentialAncestor, File file ) throws IOException { String ancestorPath = potentialAncestor.getCanonicalPath(); String path = file.getCanonicalPath(); boolean result = false; if( path.startsWith( ancestorPath )) { String s = path.substring( ancestorPath.length()); result = s.isEmpty() || s.startsWith( System.getProperty( "file.separator" )); } return result; } private  Utils(); static boolean isEmptyOrWhitespaces( String s ); static String capitalize( String s ); static String removeFileExtension( String filename ); static List<String> splitNicely( String toSplit, String separator ); static List<String> splitNicelyWithPattern( String toSplit, String patternSeparator ); static List<String> filterEmptyValues( List<String> values ); static String format( Collection<String> items, String separator ); static String expandTemplate(String s, Properties params); static void closeQuietly( InputStream in ); static void closeQuietly( OutputStream out ); static void closeQuietly( Reader reader ); static void closeQuietly( Writer writer ); static void copyStreamUnsafelyUseWithCaution( InputStream in, OutputStream os ); static void copyStreamSafely( InputStream in, ByteArrayOutputStream os ); static void copyStream( InputStream in, File outputFile ); static void copyStream( File inputFile, File outputFile ); static void copyStream( File inputFile, OutputStream os ); static void writeStringInto( String s, File outputFile ); static void appendStringInto( String s, File outputFile ); static String readFileContent( File file ); static String readFileContentQuietly( File file, Logger logger ); static String readUrlContent( String url ); static String readUrlContentQuietly( String url, Logger logger ); static Properties readPropertiesFile( File file ); static Properties readPropertiesFileQuietly( File file, Logger logger ); static Properties readPropertiesQuietly( String fileContent, Logger logger ); static void writePropertiesFile( Properties properties, File file ); static String cleanNameWithAccents( String name ); static void createDirectory( File directory ); static List<File> listAllFiles( File directory ); static String updateProperties( String propertiesContent, Map<String,String> keyToNewValue ); static List<File> listAllFiles( File directory, String fileExtension ); static List<File> listAllFiles( File directory, boolean includeDirectories ); static List<File> listDirectFiles( File directory, String fileExtension ); static List<File> listDirectories( File root ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory, List<String> exclusionPatteners ); static Map<String,String> storeDirectoryResourcesAsString( File directory ); static String computeFileRelativeLocation( File rootDirectory, File subFile ); static void extractZipArchive( File zipFile, File targetDirectory ); static void extractZipArchive( File zipFile, File targetDirectory, String entryPattern, String removedEntryPrefix ); static boolean isAncestor( File ancestorCandidate, File file ); static void deleteFilesRecursively( File... files ); static void deleteFilesRecursivelyAndQuietly( File... files ); static String writeExceptionButDoNotUseItForLogging( Throwable t ); static void logException( Logger logger, Level logLevel, Throwable t, String message ); static void logException( Logger logger, Level logLevel, Throwable t ); static void logException( Logger logger, Throwable t ); static void closeStatement( PreparedStatement ps, Logger logger ); static void closeStatement( Statement st, Logger logger ); static void closeResultSet( ResultSet resultSet, Logger logger ); static void closeConnection( Connection conn, Logger logger ); static boolean isAncestorFile( File potentialAncestor, File file ); static void copyDirectory( File source, File target ); static Map.Entry<String,Integer> findUrlAndPort( String url ); static String getValue(Map<String,String> map, String key, String defaultValue); }
Utils { public static boolean isAncestorFile( File potentialAncestor, File file ) throws IOException { String ancestorPath = potentialAncestor.getCanonicalPath(); String path = file.getCanonicalPath(); boolean result = false; if( path.startsWith( ancestorPath )) { String s = path.substring( ancestorPath.length()); result = s.isEmpty() || s.startsWith( System.getProperty( "file.separator" )); } return result; } private  Utils(); static boolean isEmptyOrWhitespaces( String s ); static String capitalize( String s ); static String removeFileExtension( String filename ); static List<String> splitNicely( String toSplit, String separator ); static List<String> splitNicelyWithPattern( String toSplit, String patternSeparator ); static List<String> filterEmptyValues( List<String> values ); static String format( Collection<String> items, String separator ); static String expandTemplate(String s, Properties params); static void closeQuietly( InputStream in ); static void closeQuietly( OutputStream out ); static void closeQuietly( Reader reader ); static void closeQuietly( Writer writer ); static void copyStreamUnsafelyUseWithCaution( InputStream in, OutputStream os ); static void copyStreamSafely( InputStream in, ByteArrayOutputStream os ); static void copyStream( InputStream in, File outputFile ); static void copyStream( File inputFile, File outputFile ); static void copyStream( File inputFile, OutputStream os ); static void writeStringInto( String s, File outputFile ); static void appendStringInto( String s, File outputFile ); static String readFileContent( File file ); static String readFileContentQuietly( File file, Logger logger ); static String readUrlContent( String url ); static String readUrlContentQuietly( String url, Logger logger ); static Properties readPropertiesFile( File file ); static Properties readPropertiesFileQuietly( File file, Logger logger ); static Properties readPropertiesQuietly( String fileContent, Logger logger ); static void writePropertiesFile( Properties properties, File file ); static String cleanNameWithAccents( String name ); static void createDirectory( File directory ); static List<File> listAllFiles( File directory ); static String updateProperties( String propertiesContent, Map<String,String> keyToNewValue ); static List<File> listAllFiles( File directory, String fileExtension ); static List<File> listAllFiles( File directory, boolean includeDirectories ); static List<File> listDirectFiles( File directory, String fileExtension ); static List<File> listDirectories( File root ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory ); static Map<String,byte[]> storeDirectoryResourcesAsBytes( File directory, List<String> exclusionPatteners ); static Map<String,String> storeDirectoryResourcesAsString( File directory ); static String computeFileRelativeLocation( File rootDirectory, File subFile ); static void extractZipArchive( File zipFile, File targetDirectory ); static void extractZipArchive( File zipFile, File targetDirectory, String entryPattern, String removedEntryPrefix ); static boolean isAncestor( File ancestorCandidate, File file ); static void deleteFilesRecursively( File... files ); static void deleteFilesRecursivelyAndQuietly( File... files ); static String writeExceptionButDoNotUseItForLogging( Throwable t ); static void logException( Logger logger, Level logLevel, Throwable t, String message ); static void logException( Logger logger, Level logLevel, Throwable t ); static void logException( Logger logger, Throwable t ); static void closeStatement( PreparedStatement ps, Logger logger ); static void closeStatement( Statement st, Logger logger ); static void closeResultSet( ResultSet resultSet, Logger logger ); static void closeConnection( Connection conn, Logger logger ); static boolean isAncestorFile( File potentialAncestor, File file ); static void copyDirectory( File source, File target ); static Map.Entry<String,Integer> findUrlAndPort( String url ); static String getValue(Map<String,String> map, String key, String defaultValue);  }