Class TARHelper

java.lang.Object
net.simplace.core.service.TARHelper

public final class TARHelper extends Object
Static helper methods for GZIP compression and TAR archives (based on Apache Commons Compress).
Author:
Andreas Enders
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addToTar(File aFileToAdd, File aSourceZipFile, File aDestinationZipFile)
    Copies all entries of a TAR file into a new TAR file and adds one further file (without path).
    static void
    addToTar(Collection<File> aFilesCollection, File aSourceTarFile, File aDestinationTarFile)
    Copies all entries of a TAR file into a new TAR file and adds further files (without paths).
    static void
    createTar(String aFileName, InputStream aInputStream, File aDestinationFile, boolean doGZIP)
    Creates a TAR file with a single entry read from an input stream.
    static void
    createTar(Collection<File> aFilesCollection, File aDestinationFile, boolean doGZIP)
    Creates a TAR file from a collection of files.
    static void
    createTARWithPathEntries(File aDirectory, File aTarFile, boolean doGZIP)
    Packs a directory recursively into a TAR file.
    static void
    createTARWithPathEntries(Collection<File> aFilesCollection, File aDestinationFile, String aParentStartPath, boolean doGZIP)
    Creates a TAR file from a collection of files, keeping the path of each file relative to a start path, so that the directory structure is restored when unpacking.
    static char
    getFileSeparator(File aZipArchive)
    Deprecated.
    reads a ZIP archive and is not used; use ZIPHelper.getFileSeparator(File)
    static Collection<org.apache.commons.compress.archivers.tar.TarArchiveEntry>
    getTAREntries(File aSourceTarFile, boolean doUnGZIP)
    Lists the entries of a TAR file.
    static File
    gz(File aSourceFile)
    Compresses a file with GZIP into <file>.gz.
    static File
    gz(File aSourceFile, boolean doDeletSourceFile)
    Compresses a file with GZIP into <file>.gz.
    static File
    gz(File aSourceFile, File aDestFile)
    Compresses a file with GZIP into the given destination file.
    static File
    gz(File aSourceFile, File aDestFile, boolean doDeletSourceFile)
    Compresses a file with GZIP into the given destination file.
    static final boolean
    packAndDeleteDirContents(File aTarFile, File aDir, String aExtension, long aOlderThanMillis, boolean doGZIP)
    Packs the matching files of a directory (not its subdirectories) into a TAR file and deletes them afterwards.
    static final boolean
    packDirContents(File aTarFile, File aDir, String aExtension, long aOlderThanMillis, boolean doGZIP)
    Packs the matching files of a directory (not its subdirectories) into a TAR file.
    static File
    ungz(File aSourceFile)
    Decompresses a GZIP file.
    static File
    ungz(File aSourceFile, boolean doDeleteSourceFile)
    Decompresses a GZIP file.
    static File
    ungz(File aSourceFile, File aDestFile)
    Decompresses a GZIP file into the given destination file.
    static File
    ungz(File aSourceFile, File aDestFile, boolean doDeleteSourceFile)
    Decompresses a GZIP file into the given destination file.
    static List<File>
    unTar(File aSourceTarFile, String aDestinationDir, boolean aReplacePercentInPath, boolean doUnGZIP)
    Unpacks a TAR file into a directory.
    static List<File>
    unTar(File aSourceTarFile, String aSubPath, String aDestinationDir, boolean doUnGZIP)
    Unpacks the entries of a TAR file that start with a given sub path into a directory.
    static List<File>
    unTar(File aSourceTarFile, String aSubPath, String aDestinationDir, boolean aReplacePercentInPath, boolean doUnGZIP)
    Unpacks the entries of a TAR file that start with a given sub path into a directory.
    static List<File>
    unTAR(File aSourceTarFile, String aDestinationDir, boolean doUnGZIP)
    Unpacks a TAR file into a directory.
    static void
    unTarWithPath(File aSourceTarFile, String aDestinationDir, boolean doUnGZIP)
    Unpacks a TAR file into a directory, restoring the paths of the entries.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • gz

      public static File gz(File aSourceFile) throws ZipException
      Compresses a file with GZIP into <file>.gz. The source file is kept.
      Parameters:
      aSourceFile - file to compress
      Returns:
      the compressed file
      Throws:
      ZipException - declared but not thrown; errors are logged
    • gz

      public static File gz(File aSourceFile, boolean doDeletSourceFile) throws ZipException
      Compresses a file with GZIP into <file>.gz. A file already named .gz is rejected (it would be compressed into itself).
      Parameters:
      aSourceFile - file to compress
      doDeletSourceFile - if true, the source file is deleted afterwards
      Returns:
      the compressed file
      Throws:
      ZipException - if the file is already named .gz; other errors are logged
    • gz

      public static File gz(File aSourceFile, File aDestFile) throws ZipException
      Compresses a file with GZIP into the given destination file. The source file is kept.
      Parameters:
      aSourceFile - file to compress
      aDestFile - compressed target file
      Returns:
      aDestFile
      Throws:
      ZipException - declared but not thrown; errors are logged
    • gz

      public static File gz(File aSourceFile, File aDestFile, boolean doDeletSourceFile) throws ZipException
      Compresses a file with GZIP into the given destination file.
      Parameters:
      aSourceFile - file to compress
      aDestFile - compressed target file
      doDeletSourceFile - if true, the source file is deleted afterwards (only if the compression succeeded)
      Returns:
      aDestFile
      Throws:
      ZipException - if source and target are the same file; other errors are logged
    • ungz

      public static File ungz(File aSourceFile) throws ZipException
      Decompresses a GZIP file. The target file name is the source name without its last extension (e.g. data.tar.gz → data.tar). The source file is kept.
      Parameters:
      aSourceFile - GZIP file to decompress
      Returns:
      the decompressed file
      Throws:
      ZipException - declared but not thrown; errors are logged
    • ungz

      public static File ungz(File aSourceFile, boolean doDeleteSourceFile) throws ZipException
      Decompresses a GZIP file. The target file name is the source name without its last extension (e.g. data.tar.gz → data.tar).
      Parameters:
      aSourceFile - GZIP file to decompress
      doDeleteSourceFile - if true, the source file is deleted afterwards
      Returns:
      the decompressed file
      Throws:
      ZipException - declared but not thrown; errors are logged
    • ungz

      public static File ungz(File aSourceFile, File aDestFile) throws ZipException
      Decompresses a GZIP file into the given destination file. The source file is kept.
      Parameters:
      aSourceFile - GZIP file to decompress
      aDestFile - decompressed target file
      Returns:
      aDestFile
      Throws:
      ZipException - declared but not thrown; errors are logged
    • ungz

      public static File ungz(File aSourceFile, File aDestFile, boolean doDeleteSourceFile) throws ZipException
      Decompresses a GZIP file into the given destination file.
      Parameters:
      aSourceFile - GZIP file to decompress
      aDestFile - decompressed target file
      doDeleteSourceFile - if true, the source file is deleted afterwards
      Returns:
      aDestFile
      Throws:
      ZipException - declared but not thrown; errors are logged
    • createTARWithPathEntries

      public static void createTARWithPathEntries(File aDirectory, File aTarFile, boolean doGZIP) throws ZipException
      Packs a directory recursively into a TAR file. Entry names are relative to the directory.
      Parameters:
      aDirectory - directory to pack
      aTarFile - TAR file to create
      doGZIP - if true, the TAR is GZIP compressed into the destination (name ending with .gz) or into <destination>.gz; no uncompressed TAR is kept
      Throws:
      ZipException - if the TAR file cannot be created
    • createTARWithPathEntries

      public static void createTARWithPathEntries(Collection<File> aFilesCollection, File aDestinationFile, String aParentStartPath, boolean doGZIP) throws ZipException
      Creates a TAR file from a collection of files, keeping the path of each file relative to a start path, so that the directory structure is restored when unpacking. Directories in the collection are added as directory entries, so empty directories are preserved.
      Parameters:
      aFilesCollection - files and directories to pack
      aDestinationFile - TAR file to create
      aParentStartPath - path prefix that is removed from the absolute file paths to build the entry names
      doGZIP - if true, the TAR is GZIP compressed into the destination (name ending with .gz) or into <destination>.gz; no uncompressed TAR is kept
      Throws:
      ZipException - if the TAR file cannot be created
    • unTarWithPath

      public static void unTarWithPath(File aSourceTarFile, String aDestinationDir, boolean doUnGZIP) throws ZipException
      Unpacks a TAR file into a directory, restoring the paths of the entries. Directory entries are created as (possibly empty) directories.
      Parameters:
      aSourceTarFile - TAR file to unpack
      aDestinationDir - target directory
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first (see ungz(File))
      Throws:
      ZipException - if the TAR file cannot be read or unpacked
    • packAndDeleteDirContents

      public static final boolean packAndDeleteDirContents(File aTarFile, File aDir, String aExtension, long aOlderThanMillis, boolean doGZIP)
      Packs the matching files of a directory (not its subdirectories) into a TAR file and deletes them afterwards. The directory itself is not deleted.
      Parameters:
      aTarFile - TAR file to create
      aDir - directory whose files are packed
      aExtension - only files with this extension are packed
      aOlderThanMillis - only files older than this age in milliseconds are packed
      doGZIP - if true, the TAR is GZIP compressed (see createTar(Collection, File, boolean))
      Returns:
      true if the files were packed and deleted
      See Also:
    • packDirContents

      public static final boolean packDirContents(File aTarFile, File aDir, String aExtension, long aOlderThanMillis, boolean doGZIP)
      Packs the matching files of a directory (not its subdirectories) into a TAR file. The files are kept.
      Parameters:
      aTarFile - TAR file to create
      aDir - directory whose files are packed
      aExtension - only files with this extension are packed
      aOlderThanMillis - only files older than this age in milliseconds are packed
      doGZIP - if true, the TAR is GZIP compressed (see createTar(Collection, File, boolean))
      Returns:
      true if the files were packed
      See Also:
    • createTar

      public static void createTar(Collection<File> aFilesCollection, File aDestinationFile, boolean doGZIP) throws ZipException
      Creates a TAR file from a collection of files. Only the file names are stored, without paths. Nothing is done if the collection is empty.
      Parameters:
      aFilesCollection - files to pack
      aDestinationFile - TAR file to create
      doGZIP - if true, the TAR is GZIP compressed into the destination (name ending with .gz) or into <destination>.gz; no uncompressed TAR is kept
      Throws:
      ZipException - if the TAR file cannot be created
    • createTar

      public static void createTar(String aFileName, InputStream aInputStream, File aDestinationFile, boolean doGZIP) throws ZipException
      Creates a TAR file with a single entry read from an input stream. The stream is read completely and closed. Nothing is done if the stream is null.
      Parameters:
      aFileName - name of the entry in the TAR file
      aInputStream - content of the entry
      aDestinationFile - TAR file to create
      doGZIP - if true, the TAR is GZIP compressed into the destination (name ending with .gz) or into <destination>.gz; no uncompressed TAR is kept
      Throws:
      ZipException - if the TAR file cannot be created
    • addToTar

      public static void addToTar(Collection<File> aFilesCollection, File aSourceTarFile, File aDestinationTarFile) throws ZipException
      Copies all entries of a TAR file into a new TAR file and adds further files (without paths). If the source TAR file does not exist or cannot be read, a new TAR file with only the given files is created.
      Parameters:
      aFilesCollection - files to add
      aSourceTarFile - existing TAR file
      aDestinationTarFile - TAR file to create; must differ from the source file
      Throws:
      ZipException - if the TAR file cannot be created
    • addToTar

      public static void addToTar(File aFileToAdd, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Copies all entries of a TAR file into a new TAR file and adds one further file (without path).
      Parameters:
      aFileToAdd - file to add
      aSourceZipFile - existing TAR file
      aDestinationZipFile - TAR file to create; must differ from the source file
      Throws:
      ZipException - if the TAR file cannot be created
      See Also:
    • unTAR

      public static List<File> unTAR(File aSourceTarFile, String aDestinationDir, boolean doUnGZIP) throws ZipException
      Unpacks a TAR file into a directory.
      Parameters:
      aSourceTarFile - TAR file to unpack
      aDestinationDir - target directory
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the TAR file cannot be read or unpacked
    • unTar

      public static List<File> unTar(File aSourceTarFile, String aDestinationDir, boolean aReplacePercentInPath, boolean doUnGZIP) throws ZipException
      Unpacks a TAR file into a directory.
      Parameters:
      aSourceTarFile - TAR file to unpack
      aDestinationDir - target directory
      aReplacePercentInPath - if true, % characters in entry names are treated as path separators
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the TAR file cannot be read or unpacked
    • unTar

      public static List<File> unTar(File aSourceTarFile, String aSubPath, String aDestinationDir, boolean doUnGZIP) throws ZipException
      Unpacks the entries of a TAR file that start with a given sub path into a directory. The sub path is removed from the entry names.
      Parameters:
      aSourceTarFile - TAR file to unpack
      aSubPath - only entries starting with this path are unpacked; empty string for all entries
      aDestinationDir - target directory
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the TAR file cannot be read or unpacked
    • unTar

      public static List<File> unTar(File aSourceTarFile, String aSubPath, String aDestinationDir, boolean aReplacePercentInPath, boolean doUnGZIP) throws ZipException
      Unpacks the entries of a TAR file that start with a given sub path into a directory. The sub path is removed from the entry names.
      Parameters:
      aSourceTarFile - TAR file to unpack
      aSubPath - only entries starting with this path are unpacked; empty string for all entries
      aDestinationDir - target directory
      aReplacePercentInPath - if true, % characters in entry names are treated as path separators
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the TAR file cannot be read or unpacked
    • getTAREntries

      public static Collection<org.apache.commons.compress.archivers.tar.TarArchiveEntry> getTAREntries(File aSourceTarFile, boolean doUnGZIP) throws ZipException
      Lists the entries of a TAR file.
      Parameters:
      aSourceTarFile - TAR file to read
      doUnGZIP - if true, the source is a GZIP compressed TAR file that is decompressed first
      Returns:
      the entries of the TAR file
      Throws:
      ZipException - if the TAR file cannot be read
    • getFileSeparator

      @Deprecated public static char getFileSeparator(File aZipArchive)
      Deprecated.
      reads a ZIP archive and is not used; use ZIPHelper.getFileSeparator(File)
      Parses the Entries of the given Zip archive and looks for the file separator character, if it is a directory. This method is useful as you don't know if the ZipArchive was packed on a Linux or on a Windows system. Both operating systems use different file separators when creating a zip archive. if it should be unzipped on the other OS, this might lead to errors.

      Note: despite the class name, this method reads a ZIP archive, not a TAR archive.

      Parameters:
      aZipArchive - ZIP archive to check
      Returns:
      the separator found in the first file entry containing one; / by default or on error