Class ZipStorage

All Implemented Interfaces:
Comparable<Storage>
Direct Known Subclasses:
PluginZipStorage

public class ZipStorage extends Storage
A persistent data storage and retrieval handler based on a ZIP file. This class will read both Java property files and binary files depending on the file extension. The property files are converted to dictionary object on retrieval.

Note: This storage is read-only. Unpack the ZIP file and use a DirStorage for read-write access.

Version:
1.0
  • Field Details

    • KEY_FILE

      public static final String KEY_FILE
      The dictionary key for the ZIP file location. The value stored is a file object.
      See Also:
    • zip

      protected ZipFile zip
      The ZIP file used for locating resources.
    • paths

      protected HashMap<Path,Path> paths
      The ZIP storage paths (identity) map. This is used to normalize paths for case-insensitive matching. HashMap objects don't allow fast access to hash keys or entries (only values), so a separate map is required.
    • entries

      protected HashMap<Path,Object> entries
      The ZIP entries and index map. Indexed by the storage path and linked to either the Index or the ZipEntry objects.
  • Constructor Details

    • ZipStorage

      public ZipStorage(File zipFile) throws IOException
      Creates a new read-only ZIP file storage.
      Parameters:
      zipFile - the ZIP file to use
      Throws:
      IOException - if the ZIP couldn't be opened properly
  • Method Details

    • init

      public final void init()
      Initializes this object. This method locates all the ZIP file entries and creates all the index objects.
      Overrides:
      init in class StorableObject
    • destroy

      public void destroy() throws StorageException
      Destroys this object. This method is used to free any resources used when this object is no longer used.
      Overrides:
      destroy in class StorableObject
      Throws:
      StorageException - if the destruction failed
    • file

      public final File file()
      Returns the ZIP file being read by this storage.
      Returns:
      the ZIP file being read
    • lookup

      public Metadata lookup(Path path)
      Searches for an object at the specified location and returns metadata about the object if found. The path may locate either an index or a specific object.
      Specified by:
      lookup in class Storage
      Parameters:
      path - the storage location
      Returns:
      the metadata for the object, or null if not found
    • load

      public Object load(Path path)
      Loads an object from the specified location. The path may locate either an index or a specific object. In case of an index, the data returned is an index dictionary listing of all objects in it.
      Specified by:
      load in class Storage
      Parameters:
      path - the storage location
      Returns:
      the data read, or null if not found
    • store

      public void store(Path path, Object data) throws StorageException
      Stores an object at the specified location. The path must locate a particular object or file, since direct manipulation of indices is not supported. Any previous data at the specified path will be overwritten or removed. Note that only dictionaries and files can be stored in a file storage.
      Specified by:
      store in class Storage
      Parameters:
      path - the storage location
      data - the data to store
      Throws:
      StorageException - if the data couldn't be written
    • remove

      public void remove(Path path) throws StorageException
      Removes an object or an index at the specified location. If the path refers to an index, all contained objects and indices will be removed recursively.
      Specified by:
      remove in class Storage
      Parameters:
      path - the storage location
      Throws:
      StorageException - if the data couldn't be removed
    • locatePath

      protected Path locatePath(Path path)
      Searches for an existing path in the ZIP file. If no exact match is found, the storage data file extensions are tried for a match.
      Parameters:
      path - the storage location
      Returns:
      an existing path in the ZIP file, or null if no match was found