Class FileUtils

java.lang.Object
overit.geocall.util.FileUtils

public class FileUtils extends Object
This class exposes a series of utility method that can be used in the File management
  • Constructor Details

    • FileUtils

      protected FileUtils()
  • Method Details

    • delete

      public static void delete(File f)
      Deletes a File
      Parameters:
      f - The File to delete
    • delete

      public static void delete(File f, boolean removeDirs, boolean followLinks) throws IOException
      Throws:
      IOException
    • fCopy

      public static boolean fCopy(File inputFile, File outputFile)
      Copies the input File in the output File
      Parameters:
      inputFile - The source file
      outputFile - The destination file
      Returns:
      true if the copy was successful, false otherwise
    • fMove

      public static boolean fMove(File inputFile, File outputFile)
      Moves a File
      Parameters:
      inputFile - The source file
      outputFile - The destination file
      Returns:
      true if the operation is successful, false otherwise
    • unzipFile

      public static WebResource unzipFile(WebResource wr)
      Unzips the WebResources passed as parameter
      Parameters:
      wr - The resource to unzip
      Returns:
      The unzipped resource
    • countZipEntryes

      public static int countZipEntryes(WebResource wr)
      Returns the number of entries of the zipped WebResource passed as parameter
      Parameters:
      wr - The zipped resource
      Returns:
      The number of entries of the zipped resource, or -1 if there is an exception during the count
    • unzipWebResource

      public static String unzipWebResource(WebResource wr, boolean creaDirectory)
      Unzips the WebResource passed as parameter
      Parameters:
      wr - The WebResource to unzip
      creaDirectory - true to create a new directory, false otherwise
      Returns:
      null if the operation is successful, the string that contains the message of the exsception if some orrer occurs
    • unzipWebResource

      public static String unzipWebResource(WebResource wr, WebResource dir, boolean creaDirectory)
      Unzips the WebResource passed as parameter
      Parameters:
      wr - The WebResource to unzip
      dir - The WebResource directory in which unzip the source
      creaDirectory - true to create a new directory, false otherwise
      Returns:
      null if the operation is successfull, the string that contains the message of the exsception if some orrer occurs
    • unzipWebResourceTree

      public static String unzipWebResourceTree(WebResource wr, boolean creaDirectory)
      Unzips the WebResource passed as parameter and all its sub tree.
      Parameters:
      wr - The WebResource to unzip
      creaDirectory - true to create a new directory, false otherwise
      Returns:
      null if the operation is successful, the string that contains the message of the exsception if some orrer occurs
    • unzipWebResourceTree

      public static String unzipWebResourceTree(WebResource wr, WebResource dir, boolean creaDirectory)
      Unzips the WebResource passed as parameter and its sub tree.
      Parameters:
      wr - The WebResource to unzip
      dir - The WebResource directory in which unzip the source
      creaDirectory - true to create a new directory, false otherwise
      Returns:
      null if the operation is successfull, the string that contains the message of the exsception if some orrer occurs
    • xlsx2csv

      public static boolean xlsx2csv(InputStream is, String sheet, OutputStream os)
      Transforms the source file, in XLSX format, in an output file in CSV format
      Parameters:
      is - The InputStream that represents the XLSX file to transform
      sheet - The string that defines the sheet name
      os - The OutputStream in which write the uncompressed file
      Returns:
      true if the operation is successful, false otherwise
    • xlsx2csv

      public static boolean xlsx2csv(InputStream is, OutputStream os)
      Transforms the source file, in XLSX format, in an output file in CSV format
      Parameters:
      is - The InputStream that represents the XLSX file to transform
      os - The OutputStream in which write the uncompressed file
      Returns:
      true if the operation is successful, false otherwise
    • getBOMInputStreamReader

      public static InputStreamReader getBOMInputStreamReader(InputStream is) throws IOException
      Parameters:
      is - The InputStream that represents the source
      Returns:
      The InputStreamReader with BOM
      Throws:
      IOException - If an error occurs an IOException will be trown
    • getBOMInputStreamReader

      public static InputStreamReader getBOMInputStreamReader(InputStream is, String defCharset) throws IOException
      Parameters:
      is - The InputStream that represents the source
      defCharset - The string that represents the default charset
      Returns:
      The InputStreamReader with BOM
      Throws:
      IOException - If an error occurs an IOException will be trown
    • getBOMOutputStreamWriter

      public static OutputStreamWriter getBOMOutputStreamWriter(OutputStream os, String charset) throws IOException
      Parameters:
      os - The OutputStreamWriter that represents the source
      charset - The string that represents the charset
      Returns:
      The OutputStreamWriter with BOM
      Throws:
      IOException - If an error occurs an IOException will be trown
    • getBOMOutputStreamWriter

      public static OutputStreamWriter getBOMOutputStreamWriter(OutputStream os, Charset charset) throws IOException
      Parameters:
      os - The OutputStreamWriter that represents the source
      charset - The charset
      Returns:
      The OutputStreamWriter with BOM
      Throws:
      IOException - If an error occurs an IOException will be trown
    • normalizeFileName

      public static String normalizeFileName(String name)
      Performs a replacement of the characters not allowed in the construction of a file name
      Parameters:
      name - The name to be normalized
      Returns:
      The normalized name
    • normalizeQueryStringName

      public static String normalizeQueryStringName(String name)
      Performs a replacement of the characters not allowed in the construction of a string that will be used as querystring value
      Parameters:
      name - the string to be normalized
      Returns:
      the normalized string
    • checkContentType

      public static boolean checkContentType(File content, String expectedType) throws IOException
      Perform a check between the real type of the content and the one that is supposed to be
      Parameters:
      content - the file to check
      expectedType - the supposed file contentType
      Returns:
      true if the types match, false otherwise
      Throws:
      IOException - in case of some problem during the content's reading
    • checkContentType

      public static boolean checkContentType(UpFile content, String expectedType) throws IOException
      Perform a check between the real type of the content and the one that is supposed to be
      Parameters:
      content - the upfile to check
      expectedType - the supposed file contentType
      Returns:
      true if the types match, false otherwise
      Throws:
      IOException - in case of some problem during the content's reading
    • checkContentType

      public static boolean checkContentType(InputStream content, String name, String expectedType) throws IOException
      Throws:
      IOException
    • getContentTypeFromStream

      public static String getContentTypeFromStream(String resourceName, InputStream is) throws IOException
      Gets the real content-type from the stream passed in Input.
      Parameters:
      resourceName - The name of the file
      is - The InputStream from which retrieve the content-type.
      Returns:
      The real content-type from the stream passed in Input.
      Throws:
      IOException
    • zip

      public static void zip(List<File> listFiles, String destZipFile) throws IOException
      Compresses a list of files to a destination zip file
      Parameters:
      listFiles - A collection of files and directories
      destZipFile - The path of the destination zip file
      Throws:
      FileNotFoundException - if the file is not found
      IOException - if an I/O error occurs
    • zip

      public static void zip(ArrayList<String> files, String destZipFile) throws IOException
      Compresses files represented in an array of paths
      Parameters:
      files - a String array containing file paths
      destZipFile - The path of the destination zip file
      Throws:
      FileNotFoundException - if the file is not found
      IOException - if an I/O error occurs
    • validateFileContentType

      public static boolean validateFileContentType(UpFile upFile, List<String> acceptedExtensions)
      Verifies that the uploaded file has a content type that conforms to the rules defined by the "accept" attribute.
      If t's not possible to find the uploaded file's contentType or the contentTypes defined by the "accept" attribute, a validation will be carried out on the type of the uploaded file based on its extension.
      Parameters:
      upFile - The uploaded file to be validated.
      acceptedExtensions - A list of contentType and extensions, separated by comma, which define the file types that will be accepted.
      Returns:
      true if the UpFile it's valid, false otherwise.
    • validateFileContentType

      public static boolean validateFileContentType(File file, List<String> acceptedExtensions)