Class FileTools

java.lang.Object
io.goobi.viewer.controller.FileTools

public final class FileTools extends Object
File I/O utilities.
  • Field Details

  • Method Details

    • getStringFromFilePath

      public static String getStringFromFilePath(String filePath) throws IOException
      getStringFromFilePath.
      Parameters:
      filePath - absolute or relative path to the file
      Returns:
      the complete file content as a string
      Throws:
      IOException - if any.
    • getStringFromFile

      public static String getStringFromFile(File file, String encoding) throws IOException
      Reads a text file and return content as String.
      Parameters:
      file - text file to read
      encoding - The character encoding to use. If null, a standard utf-8 encoding will be used
      Returns:
      the complete file content as a trimmed string
      Throws:
      IOException - if any.
    • getStringFromFile

      public static String getStringFromFile(File file, String encoding, String convertToEncoding) throws IOException
      Reads a text file and return content as String.
      Parameters:
      file - text file to read
      encoding - The character encoding to use. If null, a standard utf-8 encoding will be used
      convertToEncoding - Optional target encoding for conversion
      Returns:
      the complete file content as a trimmed string, optionally re-encoded
      Throws:
      IOException - if any.
    • getCharset

      public static String getCharset(Path file) throws IOException
      Parameters:
      file - Path to the file whose charset should be detected
      Returns:
      Charset of the given file
      Throws:
      IOException
    • getStringFromByteArray

      public static String getStringFromByteArray(byte[] bytes, String encoding)
      Reads a String from a byte array.
      Parameters:
      bytes - raw byte array to read as text
      encoding - character encoding to apply when reading bytes
      Returns:
      the string decoded from the given byte array using the specified encoding
    • 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 write
      filePath - 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 used
      append - Whether to append the text to an existing file (true), or to overwrite it (false)
      Returns:
      the written file
      Throws:
      IOException - if any.
    • decompressGzipFile

      public static void decompressGzipFile(File gzipFile, File newFile) throws IOException
      decompressGzipFile.
      Parameters:
      gzipFile - compressed source file to decompress
      newFile - destination file for the decompressed content
      Throws:
      IOException - if any.
    • compressGzipFile

      public static void compressGzipFile(File file, File gzipFile) throws IOException
      compressGzipFile.
      Parameters:
      file - source file to compress
      gzipFile - target file to write the compressed output to
      Throws:
      IOException - if any.
    • compressZipFile

      public static void compressZipFile(List<File> files, File zipFile, Integer level) throws IOException
      compressZipFile.
      Parameters:
      files - Source files
      zipFile - Target file
      level - 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 IOException
      compressZipFile.
      Parameters:
      contentMap - map of entry paths to their text content
      zipFile - target ZIP file to write
      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 IOException
      compressZipFile.
      Parameters:
      output - OutputStream to write the ZIP data into
      contentMap - map of entry paths to their text content
      level - The compression level of the zip archive. Must be an integer in the range from 0 to 9
      Throws:
      IOException - if any.
    • checkPathExistance

      public static boolean checkPathExistance(Path path, boolean create) throws IOException
      checkPathExistance.
      Parameters:
      path - filesystem path to check for existence
      create - true to create the directory if it does not exist
      Returns:
      true if the path exists (or was successfully created), false otherwise
      Throws:
      IOException - if any.
    • copyStream

      public static void copyStream(OutputStream output, InputStream input) throws IOException
      copyStream.
      Parameters:
      output - destination stream to write bytes to
      input - source stream to read bytes from
      Throws:
      IOException - if any.
    • isFolderEmpty

      public static boolean isFolderEmpty(Path folder) throws IOException
      isFolderEmpty.
      Parameters:
      folder - directory path to check for contents
      Returns:
      true if folder empty; false otherwise
      Throws:
      IOException - if any.
    • adaptPathForWindows

      public static String adaptPathForWindows(String path)
      adaptPathForWindows.
      Parameters:
      path - Absolute path to adapt
      Returns:
      Windows-compatible path on Windows; unchanged path elsewhere
    • probeContentType

      public static String probeContentType(URI uri) throws IOException
      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

      public static String getMimeTypeFromFile(Path path) throws IOException
      Throws:
      IOException
    • probeContentType

      public static String probeContentType(String content)
      Guess the content type of the given text, using URLConnection.guessContentTypeFromName(String) If no content type could be determined, 'text/plain' is assumed.
      Parameters:
      content - Text content whose MIME type should be guessed
      Returns:
      Content mime type
    • getBottomFolderFromPathString

      public static String getBottomFolderFromPathString(String pathString)
      Parses the given String as Path and returns the lowest folder name as String. Returns an empty String if the given path is empty or null
      Parameters:
      pathString - Path string from which to extract the parent folder name
      Returns:
      The folder name, or an empty String if it could not be determined
    • getFilenameFromPathString

      public static String getFilenameFromPathString(String pathString) throws FileNotFoundException
      Parses the given String as Path and returns the last path element (the filename) as String. Returns an empty String if the given path is empty or null
      Parameters:
      pathString - Path or URL string from which to extract the file name
      Returns:
      The filename, or an empty String if it could not be determined
      Throws:
      FileNotFoundException
    • getPathFromUrlString

      public static Path getPathFromUrlString(String urlString)
      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

      public static List<Path> listFiles(Path folder, DirectoryStream.Filter<Path> filter)
    • replaceExtension

      public static Path replaceExtension(Path path, String extension)
      Returns a path which equals the given path but using the given extension in place of the original one.
      Parameters:
      path - any file path
      extension - the extension, without leading '.'
      Returns:
      Given path with replaced file extension
    • isWithin

      public static boolean isWithin(Path path, Path parent)
      Check if a path is a real descendant of the parent path.
      Parameters:
      path - Path to check for containment within parent
      parent - Ancestor path that should contain path
      Returns:
      true if path is a descendant of parent, first resolving any path backtracking with '../' or similar
    • isYoungerThan

      public static boolean isYoungerThan(Path path, Path reference)
    • getDateCreated

      public static FileTime getDateCreated(Path path) throws IOException
      Throws:
      IOException
    • getDateModified

      public static FileTime getDateModified(Path path) throws IOException
      Throws:
      IOException
    • sanitizeFileName

      public static String sanitizeFileName(String fileName)
      Removes any path elements from the given file name.
      Parameters:
      fileName - File name or path to sanitize
      Returns:
      Lowest level file name