Package io.goobi.viewer.controller
Class FileTools
java.lang.Object
io.goobi.viewer.controller.FileTools
File I/O utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final DirectoryStream.Filter<Path> static final DirectoryStream.Filter<Path> -
Method Summary
Modifier and TypeMethodDescriptionstatic StringadaptPathForWindows(String path) adaptPathForWindows.static booleancheckPathExistance(Path path, boolean create) checkPathExistance.static voidcompressGzipFile(File file, File gzipFile) compressGzipFile.static voidcompressZip(OutputStream output, Map<Path, String> contentMap, Integer level) compressZipFile.static voidcompressZipFile(List<File> files, File zipFile, Integer level) compressZipFile.static voidcompressZipFile.static voidcopyStream(OutputStream output, InputStream input) copyStream.static voiddecompressGzipFile(File gzipFile, File newFile) decompressGzipFile.static BaseMimeTypegetBaseMimeType(String mimeType) static StringgetBottomFolderFromPathString(String pathString) Parses the given String asPathand returns the lowest folder name as String.static StringgetCharset(Path file) static FileTimegetDateCreated(Path path) static FileTimegetDateModified(Path path) static FilegetFileFromString(String string, String filePath, String encoding, boolean append) Simply write a String into a text file.static StringgetFilenameFromPathString(String pathString) Parses the given String asPathand returns the last path element (the filename) as String.static StringgetMimeTypeFromFile(Path path) static PathgetPathFromUrlString(String urlString) Creates a Path from the given URL in a way that word on Windows machines.static StringgetStringFromByteArray(byte[] bytes, String encoding) Reads a String from a byte arraystatic StringgetStringFromFile(File file, String encoding) Read a text file and return content as Stringstatic StringgetStringFromFile(File file, String encoding, String convertToEncoding) Read a text file and return content as Stringstatic StringgetStringFromFilePath(String filePath) getStringFromFilePath.static booleanisFolderEmpty(Path folder) isFolderEmpty.static booleanCheck if a path is a real descendant of the parent path.static booleanisYoungerThan(Path path, Path reference) listFiles(Path folder, DirectoryStream.Filter<Path> filter) static StringprobeContentType(String content) Guess the content type of the given text, usingURLConnection.guessContentTypeFromName(String)If no content type could be determined, 'text/plain' is assumedstatic StringprobeContentType(URI uri) Guess the content type (mimeType) of the resource found at the given uri.static PathreplaceExtension(Path path, String extension) Return a path which equals the given path but using the given extension in place of the original one
-
Field Details
-
IMAGE_NAME_FILTER
-
PDF_NAME_FILTER
-
-
Method Details
-
getStringFromFilePath
getStringFromFilePath.
- Parameters:
filePath- aStringobject.- Returns:
- a
Stringobject. - Throws:
IOException- if any.
-
getStringFromFile
Read a text file and return content as String- Parameters:
file- aFileobject.encoding- The character encoding to use. If null, a standard utf-8 encoding will be used- Returns:
- a
Stringobject. - Throws:
IOException- if any.
-
getStringFromFile
public static String getStringFromFile(File file, String encoding, String convertToEncoding) throws IOException Read a text file and return content as String- Parameters:
file- aFileobject.encoding- The character encoding to use. If null, a standard utf-8 encoding will be usedconvertToEncoding- Optional target encoding for conversion- Returns:
- a
Stringobject. - Throws:
IOException- if any.
-
getCharset
- Parameters:
file-- Returns:
- Charset of the given file
- Throws:
IOException
-
getStringFromByteArray
Reads a String from a byte array -
getFileFromString
public static File getFileFromString(String string, String filePath, String encoding, boolean append) throws IOException Simply write a String into a text file.- Parameters:
string- The String to writefilePath- The file path to write to (will be created if it doesn't exist)encoding- The character encoding to use. If null, a standard utf-8 encoding will be usedappend- Whether to append the text to an existing file (true), or to overwrite it (false)- Returns:
- a
Fileobject. - Throws:
IOException- if any.
-
decompressGzipFile
decompressGzipFile.
- Parameters:
gzipFile- aFileobject.newFile- aFileobject.- Throws:
IOException- if any.
-
compressGzipFile
compressGzipFile.
- Parameters:
file- aFileobject.gzipFile- aFileobject.- Throws:
IOException- if any.
-
compressZipFile
public static void compressZipFile(List<File> files, File zipFile, Integer level) throws IOException compressZipFile.
- Parameters:
files- Source fileszipFile- Target filelevel- The compression level of the zip archive. Must be an integer in the range from 0 to 9- Throws:
IOException- if any.
-
compressZipFile
public static void compressZipFile(Map<Path, String> contentMap, File zipFile, Integer level) throws IOExceptioncompressZipFile.
- Parameters:
contentMap- aMapobject.zipFile- aFileobject.level- The compression level of the zip archive. Must be an integer in the range from 0 to 9- Throws:
IOException- if any.
-
compressZip
public static void compressZip(OutputStream output, Map<Path, String> contentMap, Integer level) throws IOExceptioncompressZipFile.
- Parameters:
output-contentMap- aMapobject.level- The compression level of the zip archive. Must be an integer in the range from 0 to 9- Throws:
IOException- if any.
-
checkPathExistance
checkPathExistance.
- Parameters:
path- aPathobject.create- a boolean.- Returns:
- a boolean.
- Throws:
IOException- if any.
-
copyStream
copyStream.
- Parameters:
output- aOutputStreamobject.input- aInputStreamobject.- Throws:
IOException- if any.
-
isFolderEmpty
isFolderEmpty.
- Parameters:
folder- aPathobject.- Returns:
- true if folder empty; false otherwise
- Throws:
IOException- if any.
-
adaptPathForWindows
adaptPathForWindows.
- Parameters:
path- Absolute path to adapt- Returns:
- Windows-compatible path on Windows; unchanged path elsewhere
-
probeContentType
Guess the content type (mimeType) of the resource found at the given uri. Content type if primarily guessed from the file extension of the last url path part. If that type is 'text/plain' further analysis is done using the actual content to determine if the actual type is html or xml If the type could not be determined from the file extension, the url response header is probed to return its 'Content-type'- Parameters:
uri- uri of the resource. May be a file uri, a relative uri (then assumed to be a relative file path) or a http(s) uri- Returns:
- The most likely mimeType of the resource found at the given uri
- Throws:
IOException
-
getMimeTypeFromFile
- Throws:
IOException
-
getBaseMimeType
- Parameters:
mimeType- Full mime type- Returns:
- First part of the mime type
-
probeContentType
Guess the content type of the given text, usingURLConnection.guessContentTypeFromName(String)If no content type could be determined, 'text/plain' is assumed- Parameters:
content-- Returns:
- Content mime type
-
getBottomFolderFromPathString
Parses the given String asPathand returns the lowest folder name as String. Returns an empty String if the given path is empty or null- Parameters:
pathString-- Returns:
- The folder name, or an empty String if it could not be determined
-
getFilenameFromPathString
Parses the given String asPathand returns the last path element (the filename) as String. Returns an empty String if the given path is empty or null- Parameters:
pathString-- Returns:
- The filename, or an empty String if it could not be determined
- Throws:
FileNotFoundException
-
getPathFromUrlString
Creates a Path from the given URL in a way that word on Windows machines.- Parameters:
urlString- Relative or absolute path or URL, with or without protocol. If a URL parameter is in itself a complete URL, it must be escaped first!- Returns:
- Constructed Path
-
listFiles
-
replaceExtension
Return a path which equals the given path but using the given extension in place of the original one- Parameters:
path- any file pathextension- the extension, without leading '.'- Returns:
- Given path with replaced file extension
-
isWithin
Check if a path is a real descendant of the parent path.- Parameters:
path-parent-- Returns:
- true if path is a descendant of parent, first resolving any path backtracking with '../' or similar
-
isYoungerThan
-
getDateCreated
- Throws:
IOException
-
getDateModified
- Throws:
IOException
-