Class ZipContent

java.lang.Object
org.springframework.boot.loader.zip.ZipContent
All Implemented Interfaces:
Closeable, AutoCloseable

public final class ZipContent extends Object implements Closeable
Provides raw access to content from a regular or nested zip file. This class performs the low level parsing of a zip file and provide access to raw entry data that it contains. Unlike ZipFile, this implementation can load content from a zip file nested inside another file as long as the entry is not compressed.

In order to reduce memory consumption, this implementation stores only the hash of the entry names, the central directory offsets and the original positions. Entries are stored internally in hashCode order so that a binary search can be used to quickly find an entry by name or determine if the zip file doesn't have a given entry.

ZipContent for a typical Spring Boot application JAR will have somewhere in the region of 10,500 entries which should consume about 122K.

ZipContent results are cached and it is assumed that zip content will not change once loaded. Entries and Strings are not cached and will be recreated on each access which may produce a lot of garbage.

This implementation does not use Cleaner.Cleanable so care must be taken to release ZipContent resources. The close() method should be called explicitly or by try-with-resources. Care must be take to only call close once.

Since:
3.2.0
Author:
Phillip Webb, Andy Wilkinson
  • Method Details

    • getKind

      public ZipContent.Kind getKind()
      Return the kind of content that was loaded.
      Returns:
      the content kind
      Since:
      3.2.2
    • openRawZipData

      public CloseableDataBlock openRawZipData() throws IOException
      Open a DataBlock containing the raw zip data. For container zip files, this may be smaller than the original file since additional bytes are permitted at the front of a zip file. For nested zip files, this will be only the contents of the nest zip.

      For nested directory zip files, a virtual data block will be created containing only the relevant content.

      To release resources, the close() method of the data block should be called explicitly or by try-with-resources.

      The returned data block should not be accessed once close() has been called.

      Returns:
      the zip data
      Throws:
      IOException - on I/O error
    • size

      public int size()
      Returns the number of entries in the ZIP file.
      Returns:
      the number of entries
    • getComment

      public String getComment()
      Return the zip comment, if any.
      Returns:
      the comment or null
    • getEntry

      public ZipContent.Entry getEntry(CharSequence name)
      Return the entry with the given name, if any.
      Parameters:
      name - the name of the entry to find
      Returns:
      the entry or null
    • getEntry

      public ZipContent.Entry getEntry(CharSequence namePrefix, CharSequence name)
      Return the entry with the given name, if any.
      Parameters:
      namePrefix - an optional prefix for the name
      name - the name of the entry to find
      Returns:
      the entry or null
    • hasEntry

      public boolean hasEntry(CharSequence namePrefix, CharSequence name)
      Return if an entry with the given name exists.
      Parameters:
      namePrefix - an optional prefix for the name
      name - the name of the entry to find
      Returns:
      the entry or null
    • getEntry

      public ZipContent.Entry getEntry(int index)
      Return the entry at the specified index.
      Parameters:
      index - the entry index
      Returns:
      the entry
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds
    • getInfo

      public <I> I getInfo(Class<I> type, Function<ZipContent,I> function)
      Get or compute information based on the ZipContent.
      Type Parameters:
      I - the info type to get or compute
      Parameters:
      type - the info type to get or compute
      function - the function used to compute the information
      Returns:
      the computed or existing information
    • hasJarSignatureFile

      public boolean hasJarSignatureFile()
      Returns true if this zip contains a jar signature file (META-INF/*.DSA).
      Returns:
      if the zip contains a jar signature file
    • close

      public void close() throws IOException
      Close this jar file, releasing the underlying file if this was the last reference.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • open

      public static ZipContent open(Path path) throws IOException
      Open ZipContent from the specified path. The resulting ZipContent must be closed by the caller.
      Parameters:
      path - the zip path
      Returns:
      a ZipContent instance
      Throws:
      IOException - on I/O error
    • open

      public static ZipContent open(Path path, String nestedEntryName) throws IOException
      Open nested ZipContent from the specified path. The resulting ZipContent must be closed by the caller.
      Parameters:
      path - the zip path
      nestedEntryName - the nested entry name to open
      Returns:
      a ZipContent instance
      Throws:
      IOException - on I/O error