Class ZipUtils

java.lang.Object
de.iip_ecosphere.platform.support.ZipUtils

public class ZipUtils extends Object
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
  • Constructor Details

    • ZipUtils

      protected ZipUtils()
      Preventing external creation.
  • Method Details

    • findFile

      public static InputStream findFile(InputStream in, String name) throws IOException
      Finds a file within the ZIP/JAR file given by in. Closes in if not found.
      Parameters:
      in - the input stream containing ZIP/JAR data
      name - the name of the file within in to 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

      public static InputStream findFile(InputStream in, Predicate<ZipEntry> pred) throws IOException
      Finds a file within the ZIP/JAR file given by in. Closes in if not found.
      Parameters:
      in - the input stream containing ZIP/JAR data
      pred - 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

      public static InputStream findFile(File file, Predicate<ZipEntry> pred) throws IOException
      Finds a file within the ZIP/JAR file given by file. Same as findFile(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 file
      pred - 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

      public static InputStream findFile(File file, String name) throws IOException
      Finds a file within the ZIP/JAR file given by file. Same as findFile(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 file
      name - the name of the file within in to 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 by in. Closes in.
      Parameters:
      in - the input stream containing ZIP/JAR data
      pred - optional predicate to determine the entries to return, may be null
      consumer - called for an ZIP entry to be listed, called also for folders if accepted by pred
      Throws:
      IOException - if something I/O related fails
    • extractZip

      public static void extractZip(InputStream in, Path target) throws IOException
      Extracts a ZIP/JAR file.
      Parameters:
      in - the input stream containing the ZIP/JAR file
      target - the target path
      Throws:
      IOException - if something I/O related fails
    • inFolder

      public static Predicate<ZipEntry> inFolder(String folder)
      Returns a predicate checking whether ZipEntry is in folder. Helper for extractZip(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 file
      target - the target path
      pred - a predicate selecting ZIP entries for creation/extraction (may be null for all contents)
      Throws:
      IOException - if something I/O related fails
    • zipSlipProtect

      private static Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOException
      Protects from ZIP slip attack.
      Parameters:
      zipEntry - the ZIP entry
      targetDir - the target directory
      Returns:
      the normalized/fixed path
      Throws:
      IOException - if something I/O related fails