Package lumis.util

Class FileUtil


  • @StableMinor(version="14.2",
                 sinceVersion="4.0")
    public class FileUtil
    extends Object
    Utility methods for file manipulation.
    Since:
    4.0.0
    Version:
    $Revision: 24985 $ $Date: 2022-05-02 23:04:30 -0300 (Mon, 02 May 2022) $
    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • move

        public static boolean move​(String sourcePath,
                                   String targetPath)
      • fileExists

        public static boolean fileExists​(String filePath)
      • deleteDir

        public static boolean deleteDir​(String dir)
      • deleteFile

        public static boolean deleteFile​(String filePath)
      • deleteFile

        public static boolean deleteFile​(File file)
        Deletes the given file.
        Parameters:
        file - the file to be deleted.
        Returns:
        whether the action has been succeeded.
        Throws:
        IllegalArgumentException - if the given argument is a directory.
      • deleteDir

        public static boolean deleteDir​(String dir,
                                        boolean deleteFiles)
      • deleteDir

        public static boolean deleteDir​(File dir)
      • deleteDir

        public static boolean deleteDir​(File dir,
                                        boolean deleteFiles)
      • copyDir

        public static void copyDir​(File srcDir,
                                   File destDir)
                            throws IOException
        Copies the source folder recursively to the destination folder.
        Parameters:
        srcDir - the source folder
        destDir - the destination folder.
        Throws:
        IOException - if any error occurs during the read or write file.
        Since:
        7.1.0
      • copyFile

        public static void copyFile​(File srcFile,
                                    File destFile)
                             throws IOException
        Copies a file.
        Throws:
        IOException - if an I/O error occurs.
      • compareFiles

        public static boolean compareFiles​(File file1,
                                           File file2)
                                    throws IOException
        Compare two files.
        Parameters:
        file1 -
        file2 -
        Returns:
        true, if the files' contents are identical.
        Throws:
        IOException
      • extractZip

        @Deprecated
        public static void extractZip​(ZipInputStream zipInput,
                                      File destDir)
                               throws IOException
        Deprecated.
        Since 7.0.0, replaced by extractZip(InputStream, File) due to limitations in Java's embedded java.util.zip package on working with different zip entry names encodings.
        Extracts a zip input stream into a directory.
        Parameters:
        zipInput - the zip input stream.
        destDir - the directory where the zip is to be extracted.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        4.0.7
      • extractZip

        public static void extractZip​(InputStream inputStream,
                                      File destDir)
                               throws IOException
        Extracts an input stream in ZIP format to the file system.
        Parameters:
        inputStream - the input stream. This is expected to be the raw input stream of the bytes in ZIP format, and must not be an input stream that already does the ZIP uncompressing such as ZipInputStream.
        destDir - the file system directory where the ZIP contents are to be extracted to.
        Throws:
        IOException - if an I/O error occurred while reading the input stream or writing to the file system.
        Since:
        7.0.0
      • extractZip

        public static void extractZip​(InputStream inputStream,
                                      IFile destDir)
                               throws IOException
        Extracts an input stream in ZIP format to the file system.
        Parameters:
        inputStream - the input stream. This is expected to be the raw input stream of the bytes in ZIP format, and must not be an input stream that already does the ZIP uncompressing such as ZipInputStream.
        destDir - the file system directory where the ZIP contents are to be extracted to.
        Throws:
        IOException - if an I/O error occurred while reading the input stream or writing to the file system.
        Since:
        8.0.0
      • zipDir

        public static void zipDir​(File dirToZip,
                                  ZipOutputStream zos)
                           throws IOException
        Zips a directory recursively into the given zip output stream. The files in the given directory will be in the zip root.
        Parameters:
        dirToZip - the dir to zip.
        zos - the zip output stream.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        4.0.7
      • zipDir

        public static void zipDir​(File dirToZip,
                                  String dirZipRelativePath,
                                  ZipOutputStream zos)
                           throws IOException
        Zips a directory recursively into the given zip output stream.
        Parameters:
        dirToZip - the dir to zip.
        dirZipRelativePath - the path for the dir inside the generated zip file. If it refers to the zip root, its value must be an empty string; or it must end with slash if it refers to a directory inside the zip.
        zos - the zip output stream.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        4.0.7
      • zipDir

        public static void zipDir​(IFile dirToZip,
                                  ZipOutputStream zos)
                           throws IOException
        Zips a directory recursively into the given zip output stream. The files in the given directory will be in the zip root.
        Parameters:
        dirToZip - the dir to zip.
        zos - the zip output stream.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        8.0.0
      • zipDir

        public static void zipDir​(IFile dirToZip,
                                  String dirZipRelativePath,
                                  ZipOutputStream zos)
                           throws IOException
        Zips a directory recursively into the given zip output stream.
        Parameters:
        dirToZip - the dir to zip.
        dirZipRelativePath - the path for the dir inside the generated zip file. If it refers to the zip root, its value must be an empty string; or it must end with slash if it refers to a directory inside the zip.
        zos - the zip output stream.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        8.0.0
      • createParentDirectories

        public static void createParentDirectories​(File file)
                                            throws IOException
        Creates the directories parent of the given file. If the parent directory already exist, nothing is done.
        Parameters:
        file - the file.
        Throws:
        IOException - if it was not possible to create the directories.
        Since:
        5.0.0
      • readFile

        public static byte[] readFile​(File file)
                               throws IOException
        Returns the content of a file as an array of bytes.
        Parameters:
        file - the file.
        Returns:
        the file's content.
        Throws:
        IOException - if an I/O error occurs.
        Since:
        4.2.2
      • getFileExtension

        public static String getFileExtension​(String fileName)
        Returns the extension of given file.
        Parameters:
        fileName - the file name to used for extract the extension.
        Returns:
        the file extension.
        Since:
        7.0.0