Class TARHelper
java.lang.Object
net.simplace.core.service.TARHelper
Static helper methods for GZIP compression and TAR archives (based on Apache Commons Compress).
- Author:
- Andreas Enders
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidCopies all entries of a TAR file into a new TAR file and adds one further file (without path).static voidaddToTar(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 voidcreateTar(String aFileName, InputStream aInputStream, File aDestinationFile, boolean doGZIP) Creates a TAR file with a single entry read from an input stream.static voidcreateTar(Collection<File> aFilesCollection, File aDestinationFile, boolean doGZIP) Creates a TAR file from a collection of files.static voidcreateTARWithPathEntries(File aDirectory, File aTarFile, boolean doGZIP) Packs a directory recursively into a TAR file.static voidcreateTARWithPathEntries(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 chargetFileSeparator(File aZipArchive) Deprecated.static Collection<org.apache.commons.compress.archivers.tar.TarArchiveEntry> getTAREntries(File aSourceTarFile, boolean doUnGZIP) Lists the entries of a TAR file.static FileCompresses a file with GZIP into<file>.gz.static FileCompresses a file with GZIP into<file>.gz.static FileCompresses a file with GZIP into the given destination file.static FileCompresses a file with GZIP into the given destination file.static final booleanpackAndDeleteDirContents(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 booleanpackDirContents(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 FileDecompresses a GZIP file.static FileDecompresses a GZIP file.static FileDecompresses a GZIP file into the given destination file.static FileDecompresses a GZIP file into the given destination file.Unpacks a TAR file into a directory.Unpacks the entries of a TAR file that start with a given sub path into a directory.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.Unpacks a TAR file into a directory.static voidunTarWithPath(File aSourceTarFile, String aDestinationDir, boolean doUnGZIP) Unpacks a TAR file into a directory, restoring the paths of the entries.
-
Method Details
-
gz
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
Compresses a file with GZIP into<file>.gz. A file already named.gzis rejected (it would be compressed into itself).- Parameters:
aSourceFile- file to compressdoDeletSourceFile- iftrue, the source file is deleted afterwards- Returns:
- the compressed file
- Throws:
ZipException- if the file is already named.gz; other errors are logged
-
gz
Compresses a file with GZIP into the given destination file. The source file is kept.- Parameters:
aSourceFile- file to compressaDestFile- 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 compressaDestFile- compressed target filedoDeletSourceFile- iftrue, 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
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
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 decompressdoDeleteSourceFile- iftrue, the source file is deleted afterwards- Returns:
- the decompressed file
- Throws:
ZipException- declared but not thrown; errors are logged
-
ungz
Decompresses a GZIP file into the given destination file. The source file is kept.- Parameters:
aSourceFile- GZIP file to decompressaDestFile- 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 decompressaDestFile- decompressed target filedoDeleteSourceFile- iftrue, 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 packaTarFile- TAR file to createdoGZIP- iftrue, 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 packaDestinationFile- TAR file to createaParentStartPath- path prefix that is removed from the absolute file paths to build the entry namesdoGZIP- iftrue, 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 unpackaDestinationDir- target directorydoUnGZIP- iftrue, the source is a GZIP compressed TAR file that is decompressed first (seeungz(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 createaDir- directory whose files are packedaExtension- only files with this extension are packedaOlderThanMillis- only files older than this age in milliseconds are packeddoGZIP- iftrue, the TAR is GZIP compressed (seecreateTar(Collection, File, boolean))- Returns:
trueif 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 createaDir- directory whose files are packedaExtension- only files with this extension are packedaOlderThanMillis- only files older than this age in milliseconds are packeddoGZIP- iftrue, the TAR is GZIP compressed (seecreateTar(Collection, File, boolean))- Returns:
trueif 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 packaDestinationFile- TAR file to createdoGZIP- iftrue, 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 isnull.- Parameters:
aFileName- name of the entry in the TAR fileaInputStream- content of the entryaDestinationFile- TAR file to createdoGZIP- iftrue, 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 addaSourceTarFile- existing TAR fileaDestinationTarFile- 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 addaSourceZipFile- existing TAR fileaDestinationZipFile- 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 unpackaDestinationDir- target directorydoUnGZIP- iftrue, 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 unpackaDestinationDir- target directoryaReplacePercentInPath- iftrue,%characters in entry names are treated as path separatorsdoUnGZIP- iftrue, 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 unpackaSubPath- only entries starting with this path are unpacked; empty string for all entriesaDestinationDir- target directorydoUnGZIP- iftrue, 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 unpackaSubPath- only entries starting with this path are unpacked; empty string for all entriesaDestinationDir- target directoryaReplacePercentInPath- iftrue,%characters in entry names are treated as path separatorsdoUnGZIP- iftrue, 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 readdoUnGZIP- iftrue, 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.reads a ZIP archive and is not used; useZIPHelper.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
-
ZIPHelper.getFileSeparator(File)