Package com.globalmentor.util.zip
Class ZipManager
- java.lang.Object
-
- com.globalmentor.util.zip.ZipManager
-
- All Implemented Interfaces:
URIInputStreamable
public class ZipManager extends java.lang.Object implements URIInputStreamable
Facilitates access to a zip file. Keeps track of zip contents by zip entry name as a URI as if each zip entry were unarchived in the current location of the zip file.This class cannot descend from
ZipFile
because aZipFile
cannot be arbitrarily opened and closed.- Author:
- Garret Wilson
- See Also:
ZipFile
-
-
Constructor Summary
Constructors Constructor Description ZipManager(java.io.File zippedFile, java.net.URI baseURI)
Constructs a zip manager to manage entries in a zip file stored in the specified file.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
java.net.URI
getBaseURI()
java.io.File
getFile()
java.io.InputStream
getInputStream(java.net.URI uri)
Returns an input stream from given URI.java.net.URI
getURI(java.lang.String href)
Creates a URI from the file location, based on the zip file's URI.java.net.URI
getURI(java.util.zip.ZipEntry zipEntry)
Creates a URI to represent the given zip entry, based upon the zip entry's path relative to the URI of the zip file itself.java.util.zip.ZipEntry
getZipEntry(java.net.URI uri)
Gets the zip entry that corresponds to the given URI.java.util.Iterator<java.util.zip.ZipEntry>
getZipEntryIterator()
protected java.util.Map<java.net.URI,java.util.zip.ZipEntry>
getZipEntryMap()
java.util.zip.ZipFile
grabZipFile()
Opens the zip file if needed and increases the reference count to indicate the zip file should be held open.protected void
loadZipEntries()
Loads and maps the entries of the zip file.void
releaseZipFile()
Releases the hold on the zip file.
-
-
-
Constructor Detail
-
ZipManager
public ZipManager(java.io.File zippedFile, java.net.URI baseURI) throws java.io.IOException
Constructs a zip manager to manage entries in a zip file stored in the specified file.- Parameters:
zippedFile
- The file in which the zipped information is stored.baseURI
- The base URI to use in determining zip entry URIs.- Throws:
java.io.IOException
- Thrown if there is an error accessing the given zip file.
-
-
Method Detail
-
getFile
public java.io.File getFile()
- Returns:
- The file containing the zipped information.
-
getBaseURI
public java.net.URI getBaseURI()
- Returns:
- The base URI to use in determining zip entry URIs.
-
getZipEntryMap
protected java.util.Map<java.net.URI,java.util.zip.ZipEntry> getZipEntryMap()
- Returns:
- A map for storing zip entries, each keyed to a URI of what their filename would be if uncompressed.
-
getZipEntry
public java.util.zip.ZipEntry getZipEntry(java.net.URI uri)
Gets the zip entry that corresponds to the given URI.- Parameters:
uri
- The URI for which a URI should be returned.- Returns:
- The zip entry corresponding to the given URI, or
null
if there is no corresponding zip entry.
-
getZipEntryIterator
public java.util.Iterator<java.util.zip.ZipEntry> getZipEntryIterator()
- Returns:
- An iterator to all zip entries in the zip file in an undefined order.
-
grabZipFile
public java.util.zip.ZipFile grabZipFile() throws java.io.IOException
Opens the zip file if needed and increases the reference count to indicate the zip file should be held open. Every call to this method should eventually be followed by a single call toreleaseZipFile()
.This method is thread-safe.
- Returns:
- ZipFile An object allowing access to the zip file's contents.
- Throws:
java.io.IOException
- Thrown if there is an error opening the zip file.- See Also:
releaseZipFile()
-
releaseZipFile
public void releaseZipFile() throws java.io.IOException
Releases the hold on the zip file. When no callers hold the zip file (the hold count reaches zero), the zip file is closed and the zip file variable is set tonull
.This method is thread-safe.
- Throws:
java.io.IOException
- Thrown if there is an error closing the zip file.
-
loadZipEntries
protected void loadZipEntries() throws java.io.IOException
Loads and maps the entries of the zip file. For each zip entry, a URI is created for the canonical filename of the entry as if it were extracted with its current path under the directory of the zip file.- Throws:
java.io.IOException
- Thrown if there is an error opening and reading from the zip file.
-
close
public void close() throws java.io.IOException
- Throws:
java.io.IOException
-
getURI
public java.net.URI getURI(java.lang.String href)
Creates a URI from the file location, based on the zip file's URI.- Parameters:
href
- The location, either a URI or a filename, of the file.- Returns:
- A URI representing the specified file.
- Throws:
java.lang.IllegalArgumentException
- if the given string violates RFC 2396.- See Also:
URIs
-
getURI
public java.net.URI getURI(java.util.zip.ZipEntry zipEntry)
Creates a URI to represent the given zip entry, based upon the zip entry's path relative to the URI of the zip file itself.- Parameters:
zipEntry
- The zip entry for which a URI should be returned.- Returns:
- A URI representing the specified zip entry.
- Throws:
java.lang.IllegalArgumentException
- if the the zip entry does not have a valid filename (a URI cannot be constructed from the filename).
-
getInputStream
public java.io.InputStream getInputStream(java.net.URI uri) throws java.io.FileNotFoundException, java.io.IOException
Returns an input stream from given URI. If the URI can be matched with a zip file entry, an input stream to that entry will be returned. The input stream will be left open and should be closed after use.- Specified by:
getInputStream
in interfaceURIInputStreamable
- Parameters:
uri
- A complete URI to a file.- Returns:
- An input stream to the contents of the file represented by the given URI.
- Throws:
java.io.FileNotFoundException
- Thrown if the file referenced by the URI could not be located.java.io.IOException
- Thrown if an I/O error occurred.
-
-