Package adams.core.io
Class TarUtils
- java.lang.Object
-
- adams.core.io.TarUtils
-
public class TarUtils extends Object
A helper class for Tar-file related tasks.- Author:
- fracpete (fracpete at waikato dot ac dot nz)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TarUtils.Compression
The type of compression to use.
-
Constructor Summary
Constructors Constructor Description TarUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
compress(File output, File[] files)
Creates a tar file from the specified files.static String
compress(File output, File[] files, int bufferSize)
Creates a tar file from the specified files.static String
compress(File output, File[] files, String stripRegExp, int bufferSize)
Creates a tar file from the specified files.static List<File>
decompress(File input, File outputDir)
Decompresses the files in a tar file.static List<File>
decompress(File input, File outputDir, boolean createDirs)
Decompresses the files in a tar file.static List<File>
decompress(File input, File outputDir, boolean createDirs, BaseRegExp match, boolean invertMatch)
Decompresses the files in a tar file.static List<File>
decompress(File input, File outputDir, boolean createDirs, BaseRegExp match, boolean invertMatch, int bufferSize)
Decompresses the files in a tar file.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.static boolean
decompress(File input, String archiveFile, File output)
Decompresses the specified file from a tar file.static boolean
decompress(File input, String archiveFile, File output, boolean createDirs)
Decompresses the specified file from a tar file.static boolean
decompress(File input, String archiveFile, File output, boolean createDirs, int bufferSize, MessageCollection errors)
Decompresses the specified file from a tar file.static TarUtils.Compression
determineCompression(File archive)
Determines the compression based on the file extension.static TarUtils.Compression
determineCompression(File archive, boolean strict)
Determines the compression based on the file extension.static TarUtils.Compression
determineCompression(String archive)
Determines the compression based on the file extension.static TarUtils.Compression
determineCompression(String archive, boolean strict)
Determines the compression based on the file extension.static List<String>
getSupportedCompressionExtensions()
Returns the list of supported compression extensions.static List<File>
listFiles(File input)
Lists the files stored in the tar file.static List<File>
listFiles(File input, boolean listDirs)
Lists the files stored in the tar file.static org.apache.commons.compress.archivers.tar.TarArchiveInputStream
openArchiveForReading(File file, FileInputStream stream)
Returns an input stream for the specified tar archive.static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
openArchiveForWriting(File input, FileOutputStream stream)
Returns an output stream for the specified tar archive.
-
-
-
Field Detail
-
LOGGER
protected static Logger LOGGER
for logging errors.
-
-
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 forstrict
- 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 forstrict
- 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 forstream
- 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 forstream
- 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 generatefiles
- 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 generatefiles
- the files to store in the tar filebufferSize
- 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)
- Parameters:
output
- the output file to generatefiles
- the files to store in the tar filestripRegExp
- the regular expression used to strip the file namesbufferSize
- 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 decompressoutputDir
- 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 decompressoutputDir
- the directory where to store the extracted filescreateDirs
- 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 decompressoutputDir
- the directory where to store the extracted filescreateDirs
- whether to re-create the directory structure from the tar filematch
- the regular expression that the files are matched againstinvertMatch
- 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 decompressoutputDir
- the directory where to store the extracted filescreateDirs
- whether to re-create the directory structure from the tar filematch
- the regular expression that the files are matched againstinvertMatch
- whether to invert the matching sensebufferSize
- 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 decompressoutputDir
- the directory where to store the extracted filescreateDirs
- whether to re-create the directory structure from the tar filematch
- the regular expression that the files are matched againstinvertMatch
- whether to invert the matching sensebufferSize
- the buffer size to useerrors
- 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 decompressarchiveFile
- the file from the archive to extractoutput
- 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 decompressarchiveFile
- the file from the archive to extractoutput
- the name of the output filecreateDirs
- 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 decompressarchiveFile
- the file from the archive to extractoutput
- the name of the output filecreateDirs
- whether to create the directory structure represented by output filebufferSize
- the buffer size to useerrors
- 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
-
-