Class ZipUtils
- java.lang.Object
-
- com.opengamma.strata.collect.io.ZipUtils
-
public final class ZipUtils extends Object
Utility class to simplify accessing and creating zip files, and other packed formats.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static BeanByteSource
decryptZip(BeanByteSource source, String password)
Provides a new source that decrypts the specified source ZIP.static Map<String,ArrayByteSource>
unpackInMemory(BeanByteSource source)
Unpacks the source into memory, returning a map.static void
unpackInMemory(BeanByteSource source, BiConsumer<String,ArrayByteSource> consumer)
Unpacks the source into memory, invoking the consumer for each entry.static void
unzip(BeanByteSource source, Path path)
Unzips the source to a file path.static Map<String,ArrayByteSource>
unzipInMemory(BeanByteSource source)
Unzips the source into memory, returning a map.static void
unzipInMemory(BeanByteSource source, BiConsumer<String,ArrayByteSource> consumer)
Unzips the source into memory, invoking the consumer for each entry.static Optional<ArrayByteSource>
unzipPathNameInMemory(BeanByteSource source, String relativePathName)
Unzips a single file from the source in memory.static Set<String>
unzipPathNames(BeanByteSource source)
Unzips the source returning the file names that are contained.static ArrayByteSource
zipInMemory(List<? extends BeanByteSource> sources)
Creates a zip file from the list of files in memory.
-
-
-
Method Detail
-
unzipPathNames
public static Set<String> unzipPathNames(BeanByteSource source)
Unzips the source returning the file names that are contained.The result is a set of relative path names within the zip. Only files are returned, not folders.
- Parameters:
source
- the byte source to unzip- Returns:
- the set of relative path names
- Throws:
UncheckedIOException
- if an IO error occurs
-
unzipPathNameInMemory
public static Optional<ArrayByteSource> unzipPathNameInMemory(BeanByteSource source, String relativePathName)
Unzips a single file from the source in memory.Callers can use
unzipPathNames(BeanByteSource)
to find the available names. The relative path name must match exactly that in the zip.- Parameters:
source
- the byte source to unziprelativePathName
- the exact relative path name that the file is stored as- Returns:
- the unzipped file, empty if not found
- Throws:
UncheckedIOException
- if an IO error occurs
-
unzip
public static void unzip(BeanByteSource source, Path path)
Unzips the source to a file path.Empty folders in the zip will not be created.
- Parameters:
source
- the byte source to unzippath
- the path to unzip to- Throws:
UncheckedIOException
- if an IO error occursSecurityException
- if the path is not absolute and the calling thread cannot access system property "user.dir"
-
zipInMemory
public static ArrayByteSource zipInMemory(List<? extends BeanByteSource> sources)
Creates a zip file from the list of files in memory.Each source must have a file name. The output will not contain subfolders.
- Parameters:
sources
- the byte sources to zip- Returns:
- the zip file
- Throws:
UncheckedIOException
- if an IO error occurs
-
unpackInMemory
public static Map<String,ArrayByteSource> unpackInMemory(BeanByteSource source)
Unpacks the source into memory, returning a map.This method loads the whole unpacked file into memory. Where possible,
unpackInMemory(BeanByteSource, BiConsumer)
should be preferred as it only loads files one at a time.Unpacking handles ZIP, GZ and BASE64 formats based entirely on the suffix of the input file name. If the input suffix is not recognized as a packed format, the consumer is invoked with the original file.
- Parameters:
source
- the byte source to unpack- Returns:
- the map of unzipped byte sources, keyed by relative path name
- Throws:
UncheckedIOException
- if an IO error occurs
-
unpackInMemory
public static void unpackInMemory(BeanByteSource source, BiConsumer<String,ArrayByteSource> consumer)
Unpacks the source into memory, invoking the consumer for each entry.Unpacking handles ZIP, GZ and BASE64 formats based entirely on the suffix of the input file name. If the input suffix is not recognized as a packed format, the consumer is invoked with the original file. This method is not recursive.
- Parameters:
source
- the byte source to unpackconsumer
- the consumer, which is passed the relative path name and content for each entry- Throws:
UncheckedIOException
- if an IO error occurs
-
unzipInMemory
public static Map<String,ArrayByteSource> unzipInMemory(BeanByteSource source)
Unzips the source into memory, returning a map.This method loads the whole unpacked file into memory. Where possible,
unzipInMemory(BeanByteSource, BiConsumer)
should be preferred as it only loads files one at a time.Unlike
unpackInMemory(BeanByteSource)
, this method always treats the input as a zip file.- Parameters:
source
- the byte source to unpack- Returns:
- the map of unzipped byte sources, keyed by relative path name
- Throws:
UncheckedIOException
- if an IO error occurs
-
unzipInMemory
public static void unzipInMemory(BeanByteSource source, BiConsumer<String,ArrayByteSource> consumer)
Unzips the source into memory, invoking the consumer for each entry.Unlike
unpackInMemory(BeanByteSource, BiConsumer)
, this method always treats the input as a zip file.- Parameters:
source
- the byte source to unpackconsumer
- the consumer, which is passed the relative path name and content for each entry- Throws:
UncheckedIOException
- if an IO error occurs
-
decryptZip
public static BeanByteSource decryptZip(BeanByteSource source, String password)
Provides a new source that decrypts the specified source ZIP.This returns a wrapper around the input source that provides decryption. The result is normally passed directly into one of the other methods on this class.
- Parameters:
source
- the byte source to unpackpassword
- the password to decrypt the input- Returns:
- the decrypted zip file
-
-