Class ZipUtils
java.lang.Object
de.iip_ecosphere.platform.support.ZipUtils
Basic JAR/ZIP utilities. Streams given as parameters in this class are generic
InputStreams to be used
with class/resource loading.- Author:
- Holger Eichelberger, SSE
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classA delegating input stream that closes a given closable after closing this stream. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidextractZip(InputStream in, Path target) Extracts a ZIP/JAR file.static voidextractZip(InputStream in, Path target, Predicate<ZipEntry> pred) Extracts a ZIP/JAR file.static InputStreamFinds a file within the ZIP/JAR file given byfile.static InputStreamFinds a file within the ZIP/JAR file given byfile.static InputStreamfindFile(InputStream in, String name) Finds a file within the ZIP/JAR file given byin.static InputStreamfindFile(InputStream in, Predicate<ZipEntry> pred) Finds a file within the ZIP/JAR file given byin.Returns a predicate checking whetherZipEntryis infolder.static voidFinds a file within the ZIP/JAR file given byin.private static PathzipSlipProtect(ZipEntry zipEntry, Path targetDir) Protects from ZIP slip attack.
-
Constructor Details
-
ZipUtils
protected ZipUtils()Preventing external creation.
-
-
Method Details
-
findFile
Finds a file within the ZIP/JAR file given byin. Closesinif not found.- Parameters:
in- the input stream containing ZIP/JAR dataname- the name of the file withininto be returned- Returns:
- the input stream to
name(must be closed explicitly) or null for none - Throws:
IOException- if something I/O related fails
-
findFile
Finds a file within the ZIP/JAR file given byin. Closesinif not found.- Parameters:
in- the input stream containing ZIP/JAR datapred- the predicate to identify the file- Returns:
- the input stream to
name(must be closed explicitly) or null for none - Throws:
IOException- if something I/O related fails- See Also:
-
findFile
Finds a file within the ZIP/JAR file given byfile. Same asfindFile(InputStream, Predicate), but may work on ZIP files that have been processed as stream or Zip file system, i.e., in case of the bug that only deflated entries can have ext descriptors.- Parameters:
file- the ZIP/JAR filepred- the predicate to identify the file- Returns:
- the input stream to
name(must be closed explicitly) or null for none - Throws:
IOException- if something I/O related fails
-
findFile
Finds a file within the ZIP/JAR file given byfile. Same asfindFile(InputStream, Predicate), but may work on ZIP files that have been processed as stream or Zip file system, i.e., in case of the bug that only deflated entries can have ext descriptors.- Parameters:
file- the ZIP/JAR filename- the name of the file withininto be returned- Returns:
- the input stream to
name(must be closed explicitly) or null for none - Throws:
IOException- if something I/O related fails
-
listFiles
public static void listFiles(InputStream in, Predicate<ZipEntry> pred, Consumer<ZipEntry> consumer) throws IOException Finds a file within the ZIP/JAR file given byin. Closesin.- Parameters:
in- the input stream containing ZIP/JAR datapred- optional predicate to determine the entries to return, may be nullconsumer- called for an ZIP entry to be listed, called also for folders if accepted bypred- Throws:
IOException- if something I/O related fails
-
extractZip
Extracts a ZIP/JAR file.- Parameters:
in- the input stream containing the ZIP/JAR filetarget- the target path- Throws:
IOException- if something I/O related fails
-
inFolder
Returns a predicate checking whetherZipEntryis infolder. Helper forextractZip(InputStream, Path, Predicate).- Parameters:
folder- the folder to check for- Returns:
- the predicate
-
extractZip
public static void extractZip(InputStream in, Path target, Predicate<ZipEntry> pred) throws IOException Extracts a ZIP/JAR file.- Parameters:
in- the input stream containing the ZIP/JAR filetarget- the target pathpred- a predicate selecting ZIP entries for creation/extraction (may be null for all contents)- Throws:
IOException- if something I/O related fails
-
zipSlipProtect
Protects from ZIP slip attack.- Parameters:
zipEntry- the ZIP entrytargetDir- the target directory- Returns:
- the normalized/fixed path
- Throws:
IOException- if something I/O related fails
-