Class Archive

java.lang.Object
io.smallrye.common.io.archive.Archive

@Experimental("Incubating archive API") public final class Archive extends Object
A ZIP archive.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    compareEntryNameTo(long idx, String name)
    Compare the name of the entry at the given index to the given name.
    int
    compareEntryNameTo(long idx, String name, int offs)
    Compare the name of the entry at the given index to the given name, starting at the given offset within the entry name.
    long
    compressedSize(long idx)
    Returns the compressed size in bytes of the entry at the given index.
    creationTime(long idx)
    Returns the creation time of the entry at the given index.
    long
    Returns the number of entries in the archive.
    entryName(long idx)
    Returns the name of the entry corresponding to the given index..
    boolean
    entryNameEquals(long idx, String name)
    Determine whether the name of the entry at the given index is equal to the given name.
    boolean
    entryNameStartsWith(long idx, String prefix)
    Determine whether the name of the entry at the given index starts with the given prefix.
    long
    Find an entry with the given name.
    long
    findEntry(String name, long from, long to)
    Find an entry with the given name.
    boolean
    isDirectory(long idx)
    Determine whether the entry at the given index represents a directory.
    boolean
    isStored(long idx)
    Determine whether the entry at the given index is stored without compression.
    Map the uncompressed data of a STORED entry directly to a byte buffer.
    modifiedTime(long idx)
    Returns the last modification time of the entry at the given index.
    static Archive
    of(Path path)
    Open a ZIP archive from a filesystem file.
    static Archive
    open(byte[] bytes)
    Open a ZIP archive from a byte array.
    static Archive
    open(ByteBuffer buffer)
    Open a ZIP archive from a byte buffer.
    openEntry(long idx)
    Open the entry at the given index as an input stream.
    void
    Attempt to release some or all of the resources associated with this archive.
    storedArchive(long idx)
    Open a nested archive stored at the given index.
    long
    uncompressedSize(long idx)
    Returns the uncompressed size in bytes of the entry at the given index.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • open

      public static Archive open(byte[] bytes)
      Open a ZIP archive from a byte array.
      Parameters:
      bytes - the byte array containing the archive data (must not be null)
      Returns:
      the opened archive (not null)
      Throws:
      IllegalArgumentException - if the byte array does not contain a valid ZIP archive
    • open

      public static Archive open(ByteBuffer buffer)
      Open a ZIP archive from a byte buffer. The archive is read from the buffer's current position to its limit.
      Parameters:
      buffer - the byte buffer containing the archive data (must not be null)
      Returns:
      the opened archive (not null)
      Throws:
      IllegalArgumentException - if the buffer does not contain a valid ZIP archive
    • of

      public static Archive of(Path path) throws IOException
      Open a ZIP archive from a filesystem file. The file is mapped into memory, so normally this method should only be used for long-lived instances.
      Parameters:
      path - the path of the archive file to open (must not be null)
      Returns:
      the opened archive (not null)
      Throws:
      IOException - if the file cannot be opened
      IllegalArgumentException - if the file does not contain a valid ZIP archive
    • findEntry

      public long findEntry(String name)
      Find an entry with the given name.
      Parameters:
      name - the name of the entry (must not be null)
      Returns:
      the index of the entry, or a negative value indicating the point where the name would be found (-n - 1)
    • findEntry

      public long findEntry(String name, long from, long to)
      Find an entry with the given name.
      Parameters:
      name - the name of the entry (must not be null)
      from - the first entry to search (inclusive)
      to - the last entry to search (exclusive)
      Returns:
      the index of the entry, or a negative value indicating the point where the name would be found (-n - 1)
    • entryCount

      public long entryCount()
      Returns the number of entries in the archive.
      Returns:
      the number of entries in the archive
    • entryName

      public String entryName(long idx)
      Returns the name of the entry corresponding to the given index..
      Parameters:
      idx - the index of the entry
      Returns:
      the name of the entry corresponding to the given index.
    • entryNameEquals

      public boolean entryNameEquals(long idx, String name)
      Determine whether the name of the entry at the given index is equal to the given name. The comparison is performed directly against the encoded entry name without allocating a string.
      Parameters:
      idx - the index of the entry
      name - the name to compare against (must not be null)
      Returns:
      true if the entry name equals the given name, or false otherwise
    • entryNameStartsWith

      public boolean entryNameStartsWith(long idx, String prefix)
      Determine whether the name of the entry at the given index starts with the given prefix. The comparison is performed directly against the encoded entry name without allocating a string.
      Parameters:
      idx - the index of the entry
      prefix - the prefix to test against (must not be null)
      Returns:
      true if the entry name starts with the given prefix, or false otherwise
    • compareEntryNameTo

      public int compareEntryNameTo(long idx, String name)
      Compare the name of the entry at the given index to the given name. The comparison is performed directly against the encoded entry name without allocating a string.
      Parameters:
      idx - the index of the entry
      name - the name to compare against (must not be null)
      Returns:
      a negative value, zero, or a positive value if the entry name is less than, equal to, or greater than the given name, respectively
    • compareEntryNameTo

      public int compareEntryNameTo(long idx, String name, int offs)
      Compare the name of the entry at the given index to the given name, starting at the given offset within the entry name. The comparison is performed directly against the encoded entry name without allocating a string.
      Parameters:
      idx - the index of the entry
      name - the name to compare against (must not be null)
      offs - the character offset within the entry name at which to begin the comparison
      Returns:
      a negative value, zero, or a positive value if the entry name (from the given offset) is less than, equal to, or greater than the given name, respectively
    • storedArchive

      public Archive storedArchive(long idx)
      Open a nested archive stored at the given index.
      Parameters:
      idx - the entry index
      Returns:
      the archive (not null)
      Throws:
      IllegalArgumentException - if the zip entry is not STORED or if the entry does not appear to be a zip file
      UnsupportedOperationException - if the entry is encrypted
    • mapStoredToBuffer

      public ByteBuffer mapStoredToBuffer(long idx)
      Map the uncompressed data of a STORED entry directly to a byte buffer. Since the data is stored without compression, this provides a zero-copy view of the entry content.
      Parameters:
      idx - the entry index
      Returns:
      the byte buffer containing the entry data (not null)
      Throws:
      IllegalArgumentException - if the entry is not stored with the STORED method
      UnsupportedOperationException - if the entry is encrypted
    • openEntry

      public InputStream openEntry(long idx)
      Open the entry at the given index as an input stream. The returned stream will decompress the data if necessary. Both the STORED and DEFLATE compression methods are supported.
      Parameters:
      idx - the entry index
      Returns:
      the input stream for reading the entry data (not null)
      Throws:
      UnsupportedOperationException - if the entry uses an unsupported compression method, an unsupported version, or is encrypted
    • isStored

      public boolean isStored(long idx)
      Determine whether the entry at the given index is stored without compression.
      Parameters:
      idx - the entry index
      Returns:
      true if the entry uses the STORED compression method, or false otherwise
    • release

      public void release()
      Attempt to release some or all of the resources associated with this archive. After calling this method, accessing the archive (other than to close it) may incur some unspecified performance penalty.
    • compressedSize

      public long compressedSize(long idx)
      Returns the compressed size in bytes of the entry at the given index.
      Parameters:
      idx - the entry index
      Returns:
      the compressed size in bytes of the entry at the given index
    • uncompressedSize

      public long uncompressedSize(long idx)
      Returns the uncompressed size in bytes of the entry at the given index.
      Parameters:
      idx - the entry index
      Returns:
      the uncompressed size in bytes of the entry at the given index
    • creationTime

      public Instant creationTime(long idx)
      Returns the creation time of the entry at the given index. The time is determined by checking for NTFS and Unix extended attributes in the entry's extra field data. If no extended attributes are present, the modification date and time from the central directory entry are used as a fallback, interpreted in the system default time zone.
      Parameters:
      idx - the entry index
      Returns:
      the creation time of the entry at the given index
    • modifiedTime

      public Instant modifiedTime(long idx)
      Returns the last modification time of the entry at the given index. The time is determined by checking for NTFS and Unix extended attributes in the entry's extra field data. If no extended attributes are present, the modification date and time from the central directory entry are used as a fallback, interpreted in the system default time zone.
      Parameters:
      idx - the entry index
      Returns:
      the last modification time of the entry at the given index
    • isDirectory

      public boolean isDirectory(long idx)
      Determine whether the entry at the given index represents a directory. An entry is considered a directory if its name ends with '/'.
      Parameters:
      idx - the entry index
      Returns:
      true if the entry is a directory, or false otherwise