Class ZipUtil

java.lang.Object
eu.cloudnetservice.utils.base.io.ZipUtil

@Internal public final class ZipUtil extends Object
This zip utility class makes the process of zipping files and directories as well as extracting from zips easier and prevents malicious zip names.
Since:
4.0
  • Field Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • IS_WINDOWS

      private static final boolean IS_WINDOWS
  • Constructor Details

    • ZipUtil

      private ZipUtil()
  • Method Details

    • zipToStream

      @NonNull public static @NonNull InputStream zipToStream(@NonNull @NonNull Path directory)
      Zips the given directory into an input stream without filtering any files and returns it. This method is equivalent to ZipUtil.zipToStream(directory, null).
      Parameters:
      directory - the directory to zip.
      Returns:
      the new input stream for the zip.
      Throws:
      NullPointerException - if the given directory is null.
      IllegalStateException - if the opening of the zip file failed.
    • zipToStream

      @NonNull public static @NonNull InputStream zipToStream(@NonNull @NonNull Path directory, @Nullable @Nullable Predicate<Path> fileFilter)
      Zips the given directory into a zip input stream while filtering with the given filter and returning the new input stream.
      Parameters:
      directory - the directory to zip.
      fileFilter - the filter to filter against.
      Returns:
      the new input stream for the zip.
      Throws:
      NullPointerException - if the given directory is null.
      IllegalStateException - if the opening of the zip file failed.
    • zipToFile

      @Nullable public static @Nullable Path zipToFile(@NonNull @NonNull Path dir, @NonNull @NonNull Path target)
      Walks the file tree of the given directory and copies all files and directories without any filtering into a new zip output stream at the given target destination. This method is equivalent to ZipUtil.zipToFile(dir, target, null).
      Parameters:
      dir - the directory to zip.
      target - the destination of the zip file.
      Returns:
      the target destination on success, null if the input directory does not exist.
    • zipToFile

      @Nullable public static @Nullable Path zipToFile(@NonNull @NonNull Path dir, @NonNull @NonNull Path target, @Nullable @Nullable Predicate<Path> filter)
      Walks the file tree of the given directory and copies all files and directories that match the filter into a new zip output stream at the given target destination.
      Parameters:
      dir - the directory to zip.
      target - the destination of the zip file.
      filter - the filter to filter against.
      Returns:
      the target destination on success, null if the input directory does not exist.
    • zipDir

      private static void zipDir(@NonNull @NonNull ZipOutputStream out, @NonNull @NonNull Path dir, @Nullable @Nullable Predicate<Path> filter) throws IOException
      Walks the file tree of the given directory and copies all files and directories that match the filter into the zip output stream.
      Parameters:
      out - the stream to copy the individual zip entries to.
      dir - the directory to zip.
      filter - the filter to filter against.
      Throws:
      IOException - if the writing process of the new zip entry fails.
      NullPointerException - if the zip output stream or the directory is null.
    • extract

      @Nullable public static @Nullable Path extract(@NonNull @NonNull Path zipPath, @NonNull @NonNull Path targetDirectory)
      Extracts all entries from the zip file at the given zip path to the given target directory while catching all occurring exceptions and redirecting them into the debug log.
      Parameters:
      zipPath - the path to the zip file.
      targetDirectory - the destination to extract to.
      Returns:
      the given target directory, null if the zip path does not exist or the extraction failed.
      Throws:
      NullPointerException - if the given zip or directory path is null.
    • extract

      @Nullable public static @Nullable Path extract(@NonNull @NonNull InputStream in, @NonNull @NonNull Path targetDirectory)
      Extracts all entries from the given input stream to the given target directory while catching all occurring exceptions and redirecting them into the debug log.

      Note: If the given input stream is not a zip input stream, the stream is wrapped into one.

      Parameters:
      in - the input stream to extract from.
      targetDirectory - the destination to extract to.
      Returns:
      the given target directory on success, null if the extraction failed.
      Throws:
      NullPointerException - if the given input stream or directory is null.
    • extractZipStream

      @Nullable public static @Nullable Path extractZipStream(@NonNull @NonNull ZipInputStream zipInputStream, @NonNull @NonNull Path targetDirectory)
      Extracts all entries from the zip stream to the given target directory while catching all occurring exceptions and redirecting them into the debug log.
      Parameters:
      zipInputStream - the input stream to extract from.
      targetDirectory - the destination to extract to.
      Returns:
      the given target directory on success, null if the extraction failed.
      Throws:
      NullPointerException - if the given zip input stream or directory is null.
    • extractEntry

      private static void extractEntry(@NonNull @NonNull ZipInputStream in, @NonNull @NonNull ZipEntry zipEntry, @NonNull @NonNull Path targetDirectory) throws IOException
      Extracts the zip entry from the given zip input stream and copies it to the given destination.

      Note: If the zip entry is a directory the content of the directory is not extracted.

      Parameters:
      in - the zip input stream to extract the entry from.
      zipEntry - the entry to extract from the zip stream.
      targetDirectory - the target destination of the extracted entry.
      Throws:
      IOException - if the creation of the output stream for the entry fails.
      IllegalStateException - if the zip entry has a malicious name.
      NullPointerException - if the given input stream, zip entry or target directory is null.
    • ensureSafeZipEntryName

      private static void ensureSafeZipEntryName(@NonNull @NonNull String name)
      Ensures that the given name does not contain any characters that might lead to path traversal or other malicious behavior.
      Parameters:
      name - the name to check.
      Throws:
      NullPointerException - if the given name is null.
      IllegalStateException - if the name contains an illegal character.