Class ZipContent
- All Implemented Interfaces:
Closeable
,AutoCloseable
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
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
A single zip content entry.static enum
Zip content kinds. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close this jar file, releasing the underlying file if this was the last reference.Return the zip comment, if any.getEntry
(int index) Return the entry at the specified index.getEntry
(CharSequence name) Return the entry with the given name, if any.getEntry
(CharSequence namePrefix, CharSequence name) Return the entry with the given name, if any.<I> I
getInfo
(Class<I> type, Function<ZipContent, I> function) Get or compute information based on theZipContent
.getKind()
Return the kind of content that was loaded.boolean
hasEntry
(CharSequence namePrefix, CharSequence name) Return if an entry with the given name exists.boolean
Returnstrue
if this zip contains a jar signature file (META-INF/*.DSA
).static ZipContent
OpenZipContent
from the specified path.static ZipContent
Open nestedZipContent
from the specified path.Open aDataBlock
containing the raw zip data.int
size()
Returns the number of entries in the ZIP file.toString()
-
Method Details
-
getKind
Return the kind of content that was loaded.- Returns:
- the content kind
- Since:
- 3.2.2
-
openRawZipData
Open aDataBlock
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
Return the zip comment, if any.- Returns:
- the comment or
null
-
getEntry
Return the entry with the given name, if any.- Parameters:
name
- the name of the entry to find- Returns:
- the entry or
null
-
getEntry
Return the entry with the given name, if any.- Parameters:
namePrefix
- an optional prefix for the namename
- the name of the entry to find- Returns:
- the entry or
null
-
hasEntry
Return if an entry with the given name exists.- Parameters:
namePrefix
- an optional prefix for the namename
- the name of the entry to find- Returns:
- the entry or
null
-
getEntry
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
Get or compute information based on theZipContent
.- Type Parameters:
I
- the info type to get or compute- Parameters:
type
- the info type to get or computefunction
- the function used to compute the information- Returns:
- the computed or existing information
-
hasJarSignatureFile
public boolean hasJarSignatureFile()Returnstrue
if this zip contains a jar signature file (META-INF/*.DSA
).- Returns:
- if the zip contains a jar signature file
-
close
Close this jar file, releasing the underlying file if this was the last reference.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- See Also:
-
toString
-
open
- Parameters:
path
- the zip path- Returns:
- a
ZipContent
instance - Throws:
IOException
- on I/O error
-
open
Open nestedZipContent
from the specified path. The resultingZipContent
must beclosed
by the caller.- Parameters:
path
- the zip pathnestedEntryName
- the nested entry name to open- Returns:
- a
ZipContent
instance - Throws:
IOException
- on I/O error
-