Package adams.core.io

Class TarUtils


  • public class TarUtils
    extends Object
    A helper class for Tar-file related tasks.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • LOGGER

        protected static Logger LOGGER
        for logging errors.
    • Constructor Detail

      • TarUtils

        public TarUtils()
    • Method Detail

      • getSupportedCompressionExtensions

        public static List<String> getSupportedCompressionExtensions()
        Returns the list of supported compression extensions.
        Returns:
        the list of extensions
      • determineCompression

        public static TarUtils.Compression determineCompression​(File archive)
                                                         throws Exception
        Determines the compression based on the file extension. Falls back on "no" compression if unknown extension.
        Parameters:
        archive - the archive to determine the compression for
        Returns:
        the compression
        Throws:
        Exception - if unsupported extension
      • determineCompression

        public static TarUtils.Compression determineCompression​(File archive,
                                                                boolean strict)
                                                         throws Exception
        Determines the compression based on the file extension.
        Parameters:
        archive - the archive to determine the compression for
        strict - fail if unsupported extension
        Returns:
        the compression
        Throws:
        Exception - if unsupported extension
      • determineCompression

        public static TarUtils.Compression determineCompression​(String archive)
                                                         throws Exception
        Determines the compression based on the file extension. Falls back on "no" compression if unknown extension.
        Parameters:
        archive - the archive to determine the compression for
        Returns:
        the compression
        Throws:
        Exception - if unsupported extension
      • determineCompression

        public static TarUtils.Compression determineCompression​(String archive,
                                                                boolean strict)
                                                         throws Exception
        Determines the compression based on the file extension.
        Parameters:
        archive - the archive to determine the compression for
        strict - fail if unsupported extension
        Returns:
        the compression
        Throws:
        Exception - if unsupported extension
      • openArchiveForReading

        public static org.apache.commons.compress.archivers.tar.TarArchiveInputStream openArchiveForReading​(File file,
                                                                                                            FileInputStream stream)
                                                                                                     throws Exception
        Returns an input stream for the specified tar archive. Automatically determines the compression used for the archive.
        Parameters:
        file - the tar archive to create the input stream for
        stream - the stream to wrap
        Returns:
        the input stream
        Throws:
        Exception - if file not found or similar problems
      • openArchiveForWriting

        public static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream openArchiveForWriting​(File input,
                                                                                                             FileOutputStream stream)
                                                                                                      throws Exception
        Returns an output stream for the specified tar archive. Automatically determines the compression used for the archive. Uses GNU long filename support.
        Parameters:
        input - the tar archive to create the output stream for
        stream - the output stream to wrap
        Returns:
        the output stream
        Throws:
        Exception - if file not found or similar problems
        See Also:
        TarArchiveOutputStream.LONGFILE_GNU
      • compress

        public static String compress​(File output,
                                      File[] files)
        Creates a tar file from the specified files.
        Parameters:
        output - the output file to generate
        files - the files to store in the tar file
        Returns:
        null if successful, otherwise error message
      • compress

        public static String compress​(File output,
                                      File[] files,
                                      int bufferSize)
        Creates a tar file from the specified files.
        Parameters:
        output - the output file to generate
        files - the files to store in the tar file
        bufferSize - the buffer size to use
        Returns:
        null if successful, otherwise error message
      • compress

        public static String compress​(File output,
                                      File[] files,
                                      String stripRegExp,
                                      int bufferSize)
        Creates a tar file from the specified files.

        See Creating a tar.gz with commons-compress.
        Parameters:
        output - the output file to generate
        files - the files to store in the tar file
        stripRegExp - the regular expression used to strip the file names
        bufferSize - the buffer size to use
        Returns:
        null if successful, otherwise error message
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir)
        Decompresses the files in a tar file. Does not recreate the directory structure stored in the tar file.
        Parameters:
        input - the tar file to decompress
        outputDir - the directory where to store the extracted files
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs)
        Decompresses the files in a tar file.
        Parameters:
        input - the tar file to decompress
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the tar file
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch)
        Decompresses the files in a tar file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the tar file to decompress
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the tar file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch,
                                            int bufferSize)
        Decompresses the files in a tar file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the tar file to decompress
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the tar file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        bufferSize - the buffer size to use
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch,
                                            int bufferSize,
                                            MessageCollection errors)
        Decompresses the files in a tar file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the tar file to decompress
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the tar file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        bufferSize - the buffer size to use
        errors - for storing potential errors
        Returns:
        the successfully extracted files
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output)
        Decompresses the specified file from a tar file. Does not create any directories in case the parent directories of "output" don't exist yet.
        Parameters:
        input - the tar file to decompress
        archiveFile - the file from the archive to extract
        output - the name of the output file
        Returns:
        whether file was successfully extracted
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output,
                                         boolean createDirs)
        Decompresses the specified file from a tar file.
        Parameters:
        input - the tar file to decompress
        archiveFile - the file from the archive to extract
        output - the name of the output file
        createDirs - whether to create the directory structure represented by output file
        Returns:
        whether file was successfully extracted
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output,
                                         boolean createDirs,
                                         int bufferSize,
                                         MessageCollection errors)
        Decompresses the specified file from a tar file.
        Parameters:
        input - the tar file to decompress
        archiveFile - the file from the archive to extract
        output - the name of the output file
        createDirs - whether to create the directory structure represented by output file
        bufferSize - the buffer size to use
        errors - for storing potential errors
        Returns:
        whether file was successfully extracted
      • listFiles

        public static List<File> listFiles​(File input)
        Lists the files stored in the tar file. Lists directories automatically.
        Parameters:
        input - the tar file to obtain the file list from
        Returns:
        the stored files
      • listFiles

        public static List<File> listFiles​(File input,
                                           boolean listDirs)
        Lists the files stored in the tar file.
        Parameters:
        input - the tar file to obtain the file list from
        listDirs - whether to include directories in the list
        Returns:
        the stored files