Package com.gengoai.io
Class FileUtils
- java.lang.Object
-
- com.gengoai.io.FileUtils
-
public final class FileUtils extends Object
A set of convenience methods for handling files and file names.- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
addTrailingSlashIfNeeded(String directory)
Adds a trailing slash if its needed.static String
baseName(String file)
Attempts to get the base name for a given file/directory.static String
baseName(String file, String suffix)
Attempts to get the base name for a given file/directory.static void
copyWithProgress(@NonNull InputStream in, @NonNull Resource out, double inSize, @NonNull IntConsumer progress)
static Pattern
createFilePattern(String filePattern)
Create file pattern pattern.static String
directory(String file)
Attempts to get the directory for a given file.static String
extension(String file)
Attempts to get the file extension for a given file.static void
extract(@NonNull File source, @NonNull File target)
Extracts a compressed and/or archived file from the given source to the given target.static void
extract(@NonNull File source, @NonNull File target, @NonNull BiConsumer<String,Integer> progress)
Extracts a compressed and/or archived file from the given source to the given target.static String
parent(String file)
Returns the parent directory for the given file.static String
path(String file)
Returns the path of the file in the same manner asFile.getPath()
.static String
toUnix(String spec)
Converts file spec to unix path separatorsstatic String
toWindows(String spec)
Converts file spec to windows path separatorsstatic Resource
zip(File zipFile, Resource... entries)
Creates a zip file using the given file containing the contents of the given Resource entries.static Resource
zip(File zipFile, Map.Entry<String,Resource>... entries)
Creates a zip file using the given file containing the contents of the given Resource entries where the entries are map entries (or Tuple2) of entry name and entry resource.
-
-
-
Method Detail
-
copyWithProgress
public static void copyWithProgress(@NonNull @NonNull InputStream in, @NonNull @NonNull Resource out, double inSize, @NonNull @NonNull IntConsumer progress) throws IOException
- Throws:
IOException
-
extract
public static void extract(@NonNull @NonNull File source, @NonNull @NonNull File target) throws IOException
Extracts a compressed and/or archived file from the given source to the given target.- Parameters:
source
- the source (compressed and/or archived) filetarget
- the target directory or file to store the content- Throws:
IOException
- Something went wrong during extraction
-
extract
public static void extract(@NonNull @NonNull File source, @NonNull @NonNull File target, @NonNull @NonNull BiConsumer<String,Integer> progress) throws IOException
Extracts a compressed and/or archived file from the given source to the given target.- Parameters:
source
- the source (compressed and/or archived) filetarget
- the target directory or file to store the content- Throws:
IOException
- Something went wrong during extraction
-
addTrailingSlashIfNeeded
public static String addTrailingSlashIfNeeded(String directory)
Adds a trailing slash if its needed. Tries to determine the slash style, but defaults to unix. Assumes that what is passed in is a directory.- Parameters:
directory
- The directory to possibly add a trailing slash to- Returns:
- A directory name with a trailing slash
-
baseName
public static String baseName(String file)
Attempts to get the base name for a given file/directory. This command should work in the same way as the unix
basename
command.- Parameters:
file
- The file path/name- Returns:
- The file name if given a file, the directory name if given a directory, or null if given a null or empty string.
-
baseName
public static String baseName(String file, String suffix)
Attempts to get the base name for a given file/directory. Removes the suffix from the name as well. This command should work in the same way as the unix
basename
command.- Parameters:
file
- The file path/namesuffix
- The suffix to remove- Returns:
- The file name if given a file, the directory name if given a directory, or null if given a null or empty string.
-
createFilePattern
public static Pattern createFilePattern(String filePattern)
Create file pattern pattern.- Parameters:
filePattern
- the file pattern- Returns:
- the pattern
-
directory
public static String directory(String file)
Attempts to get the directory for a given file. Will return itself if the passed in file is a directory. Will always return a trailing slash.
- Parameters:
file
- The file- Returns:
- The path of the file spec or null if it is null
-
extension
public static String extension(String file)
Attempts to get the file extension for a given file.
- Parameters:
file
- The file- Returns:
- The file extension of the file spec or null if it is null
-
parent
public static String parent(String file)
Returns the parent directory for the given file. If the file passed in is actually a directory it will get the directory's parent.- Parameters:
file
- The file- Returns:
- The parent or null if the file is null or empty
-
path
public static String path(String file)
Returns the path of the file in the same manner asFile.getPath()
.- Parameters:
file
- The file- Returns:
- The path or null if the file is null or empty
-
toUnix
public static String toUnix(String spec)
Converts file spec to unix path separators- Parameters:
spec
- The file spec- Returns:
- Unix style path spec
-
toWindows
public static String toWindows(String spec)
Converts file spec to windows path separators- Parameters:
spec
- The file spec- Returns:
- windows style path spec
-
zip
public static Resource zip(File zipFile, Resource... entries) throws IOException
Creates a zip file using the given file containing the contents of the given Resource entries. Resource entries that do no have a validbasename
have a random 10 digit hex name generated.- Parameters:
zipFile
- the zip fileentries
- the entries- Returns:
- the resource
- Throws:
IOException
- Something went wrong creating the zip file
-
zip
@SafeVarargs public static Resource zip(File zipFile, Map.Entry<String,Resource>... entries) throws IOException
Creates a zip file using the given file containing the contents of the given Resource entries where the entries are map entries (or Tuple2) of entry name and entry resource. If null entry name is given, thebasename
of the Resource is used.- Parameters:
zipFile
- the zip fileentries
- the entries- Returns:
- the resource
- Throws:
IOException
- Something went wrong creating the zip file
-
-