adams.core.io
Class TarUtils

java.lang.Object
  extended by adams.core.io.TarUtils

public class TarUtils
extends Object

A helper class for Tar-file related tasks.

Version:
$Revision: 5957 $
Author:
fracpete (fracpete at waikato dot ac dot nz)

Nested Class Summary
static class TarUtils.Compression
          The type of compression to use.
 
Constructor Summary
TarUtils()
           
 
Method Summary
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 Vector<File> decompress(File input, File outputDir)
          Decompresses the files in a tar file.
static Vector<File> decompress(File input, File outputDir, boolean createDirs)
          Decompresses the files in a tar file.
static Vector<File> decompress(File input, File outputDir, boolean createDirs, BaseRegExp match, boolean invertMatch)
          Decompresses the files in a tar file.
static Vector<File> decompress(File input, File outputDir, boolean createDirs, BaseRegExp match, boolean invertMatch, int bufferSize)
          Decompresses the files in a tar file.
static Vector<File> decompress(File input, File outputDir, boolean createDirs, BaseRegExp match, boolean invertMatch, int bufferSize, StringBuilder 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, StringBuilder errors)
          Decompresses the specified file from a tar file.
static TarUtils.Compression determineCompression(File archive)
          Determines the compression based on the the file extension.
static TarUtils.Compression determineCompression(String archive)
          Determines the compression based on the the file extension.
static Vector<File> listFiles(File input)
          Lists the files stored in the tar file.
static Vector<File> listFiles(File input, boolean listDirs)
          Lists the files stored in the tar file.
protected static org.apache.commons.compress.archivers.tar.TarArchiveInputStream openArchiveForReading(File input)
          Returns an input stream for the specified tar archive.
protected static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream openArchiveForWriting(File input)
          Returns an output stream for the specified tar archive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TarUtils

public TarUtils()
Method Detail

determineCompression

public static TarUtils.Compression determineCompression(File archive)
Determines the compression based on the the file extension.

Parameters:
archive - the archive to determine the compression for
Returns:
the compression

determineCompression

public static TarUtils.Compression determineCompression(String archive)
Determines the compression based on the the file extension.

Parameters:
archive - the archive to determine the compression for
Returns:
the compression

openArchiveForReading

protected static org.apache.commons.compress.archivers.tar.TarArchiveInputStream openArchiveForReading(File input)
                                                                                                throws Exception
Returns an input stream for the specified tar archive. Automatically determines the compression used for the archive.

Parameters:
input - the tar archive to create the input stream for
Returns:
the input stream
Throws:
Exception - if file not found or similar problems

openArchiveForWriting

protected static org.apache.commons.compress.archivers.tar.TarArchiveOutputStream openArchiveForWriting(File input)
                                                                                                 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
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
See Also:
compress(File, File[], int)

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
See Also:
compress(File, File[], String, int)

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 Vector<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
See Also:
decompress(File, File, boolean)

decompress

public static Vector<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
See Also:
#decompress(File, File, boolean, String, boolean)

decompress

public static Vector<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
See Also:
#decompress(File, File, boolean, String, boolean, int)

decompress

public static Vector<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 Vector<File> decompress(File input,
                                      File outputDir,
                                      boolean createDirs,
                                      BaseRegExp match,
                                      boolean invertMatch,
                                      int bufferSize,
                                      StringBuilder 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,
                                 StringBuilder 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 Vector<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 Vector<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


Copyright © 2012 University of Waikato, Hamilton, NZ. All Rights Reserved.