Package org.gbif.utils.file
Class CompressionUtil
java.lang.Object
org.gbif.utils.file.CompressionUtil
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic FiledecompressFile(File compressedFile) Tries to decompress a file into a newly created temporary directory, trying gzip or zip regardless of the filename or its suffix.decompressFile(File directory, File compressedFile) Defaults keeping subDirectories to false.decompressFile(File directory, File compressedFile, boolean keepSubdirectories) Defaults keeping subDirectories to false and unzipped file name to null.decompressFile(File directory, File compressedFile, String unzippedFileName) Defaults keeping subDirectories to false, but provides unzipped file name.decompressFile(File directory, File compressedFile, String unzippedFileName, boolean keepSubdirectories) Tries to decompress a file using TAR+gzip, gzip, TAR or Zip regardless of the filename or its suffix.static booleanisGzipFormat(File compressedFile) Check the file's first two bytes, to see if they are the gzip magic number.static booleanisTarFormat(File compressedFile) Check the file is a Tape ARchive (TAR).ungzipFile(File directory, File gzipFile, boolean isTarred) Gunzip a file.ungzipFile(File directory, File gzipFile, String unzippedName, boolean isTarred) Gunzip a file.Extracts a plain TAR file.Extracts a gzipped TAR file.Defaults keepSubdirectories to false.Extracts a zipped file into a target directory.static voidZip a directory with all files but skipping included subdirectories.static voidZip a directory with all files.static voidstatic voidzipFiles(Collection<File> files, File rootContext, File zipFile) Creates a zip archive from a given collection of files.
-
Method Details
-
decompressFile
public static File decompressFile(File compressedFile) throws IOException, CompressionUtil.UnsupportedCompressionType Tries to decompress a file into a newly created temporary directory, trying gzip or zip regardless of the filename or its suffix.- Returns:
- folder containing all decompressed files
- Throws:
IOExceptionCompressionUtil.UnsupportedCompressionType
-
decompressFile
public static List<File> decompressFile(File directory, File compressedFile) throws IOException, CompressionUtil.UnsupportedCompressionType Defaults keeping subDirectories to false. -
decompressFile
public static List<File> decompressFile(File directory, File compressedFile, String unzippedFileName) throws IOException, CompressionUtil.UnsupportedCompressionType Defaults keeping subDirectories to false, but provides unzipped file name. -
decompressFile
public static List<File> decompressFile(File directory, File compressedFile, boolean keepSubdirectories) throws IOException, CompressionUtil.UnsupportedCompressionType Defaults keeping subDirectories to false and unzipped file name to null. -
decompressFile
public static List<File> decompressFile(File directory, File compressedFile, String unzippedFileName, boolean keepSubdirectories) throws IOException, CompressionUtil.UnsupportedCompressionType Tries to decompress a file using TAR+gzip, gzip, TAR or Zip regardless of the filename or its suffix.- Parameters:
directory- directory where archive's contents will be decompressed tocompressedFile- compressed fileunzippedFileName- unzipped file name (in case of one file)- Returns:
- list of files that have been extracted or null an empty list if archive couldn't be decompressed
- Throws:
IOException- if problem occurred reading compressed file, or directory couldn't be written toCompressionUtil.UnsupportedCompressionType- if the compression type wasn't recognized
-
isGzipFormat
Check the file's first two bytes, to see if they are the gzip magic number.- Parameters:
compressedFile- compressed file- Returns:
- true if the file is in gzip format
- Throws:
IOException- if a problem occurred reading compressed file
-
isTarFormat
Check the file is a Tape ARchive (TAR).- Parameters:
compressedFile- compressed file- Returns:
- true if the file is a TAR
- Throws:
IOException- if a problem occurred reading compressed file
-
untgzFile
Extracts a gzipped TAR file. Directory structure and hidden files (i.e. files starting with a dot) are ignored.- Parameters:
directory- where the file should be extracted totgzFile- to extract- Returns:
- a list of all created files
- Throws:
IOException
-
untarFile
Extracts a plain TAR file. Directory structure and hidden files (i.e. files starting with a dot) are ignored.- Parameters:
directory- where the file should be extracted totarFile- to extract- Returns:
- a list of all created files
- Throws:
IOException
-
ungzipFile
public static List<File> ungzipFile(File directory, File gzipFile, boolean isTarred) throws IOException Gunzip a file. Use this method with isTarred false if the gzip contains a single file. If it's a gzip of a TAR pass true to isTarred (or calluntgzFile(File, File)which is what this method just redirects to for isTarred).- Parameters:
directory- the output directory for the uncompressed file(s)gzipFile- the gzip fileisTarred- true if the gzip contains a TAR- Returns:
- a List of the uncompressed file name(s)
- Throws:
IOException- if reading or writing fails
-
ungzipFile
public static List<File> ungzipFile(File directory, File gzipFile, String unzippedName, boolean isTarred) throws IOException Gunzip a file. Similar toungzipFile(File, File, boolean), but with ability to control unzipped file name.- Parameters:
directory- the output directory for the uncompressed file(s)gzipFile- the gzip fileunzippedName- the unzipped fileisTarred- true if the gzip contains a TAR- Returns:
- a List of the uncompressed file name(s)
- Throws:
IOException- if reading or writing fails
-
unzipFile
Defaults keepSubdirectories to false.- Throws:
IOException- See Also:
-
zipDir
Zip a directory with all files but skipping included subdirectories. Only files directly within the directory are added to the archive.- Parameters:
dir- the directory to zipzipFile- the zipped file- Throws:
IOException
-
zipDir
Zip a directory with all files. Files in Subdirectories will be included if the inclSubdirs is true.- Parameters:
dir- the directory to zipzipFile- the zipped fileinclSubdirs- if true includes all subdirectories recursively- Throws:
IOException
-
zipFile
- Throws:
IOException
-
zipFiles
public static void zipFiles(Collection<File> files, File rootContext, File zipFile) throws IOException Creates a zip archive from a given collection of files. In order to preserve paths in the archive a rootContext can be specified which will be removed from the individual zip entries. For example a rootContext of /home/freak with a file /home/freak/photo/birthday.jpg to be zipped will result in a zip entry with a path photo/birthday.jpg.- Parameters:
files- to be included in the zip archiverootContext- optional path to be removed from each filezipFile- the zip file to be created- Throws:
IOException
-
unzipFile
public static List<File> unzipFile(File directory, File zipFile, boolean keepSubdirectories) throws IOException Extracts a zipped file into a target directory. If the file is wrapped in a root directory, this is removed by default. Other subdirectories are ignored according to the parameter keepSubdirectories. The following types of files are also ignored by default: i) hidden files (i.e. files starting with a dot) ii) Apple resource fork (__MACOSX), including its subdirectories and subfiles- Parameters:
directory- where the zipped file and its subdirectories should be extracted tozipFile- to extractkeepSubdirectories- whether to preserve subdirectories or not- Returns:
- a list of all created files and directories extracted to target directory
- Throws:
IOException
-