Class ZipFileAccess

java.lang.Object
dev.jcputney.elearning.parser.impl.access.ZipFileAccess
All Implemented Interfaces:
FileAccess, AutoCloseable

public final class ZipFileAccess extends Object implements FileAccess, AutoCloseable
An implementation of the FileAccess interface for accessing files within a ZIP archive. This class allows file existence checks, file listing, and retrieving file contents from a ZIP file.
  • Constructor Details

    • ZipFileAccess

      public ZipFileAccess(String zipFilePath) throws IOException
      Constructs a new ZipFileAccess instance for the specified ZIP path.
      Parameters:
      zipFilePath - The path to the ZIP file.
      Throws:
      IOException - If the ZIP file can't be opened.
  • Method Details

    • fileExistsInternal

      public boolean fileExistsInternal(String path)
      Checks if a file exists within the ZIP archive.
      Specified by:
      fileExistsInternal in interface FileAccess
      Parameters:
      path - The path to check (guaranteed to be non-null).
      Returns:
      True if the file exists in the ZIP archive, false otherwise.
    • listFilesInternal

      public List<String> listFilesInternal(String directoryPath)
      Lists all files within a specified directory in the ZIP archive.
      Specified by:
      listFilesInternal in interface FileAccess
      Parameters:
      directoryPath - The directory to list files from, for example, "folder/" (guaranteed to be non-null).
      Returns:
      A list of file paths within the directory, relative to the root path.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path) throws IOException
      Retrieves the contents of a file within the ZIP archive as an InputStream.
      Specified by:
      getFileContentsInternal in interface FileAccess
      Parameters:
      path - The path to retrieve contents from (guaranteed to be non-null).
      Returns:
      An InputStream of the file contents.
      Throws:
      IOException - if the file can't be read.
    • getFileContentsInternal

      public InputStream getFileContentsInternal(String path, StreamingProgressListener progressListener) throws IOException
      Retrieves the contents of a file within the ZIP archive as an InputStream with optional progress tracking.
      Parameters:
      path - The path to retrieve contents from (guaranteed to be non-null).
      progressListener - Optional progress listener for tracking large file operations.
      Returns:
      An InputStream of the file contents.
      Throws:
      IOException - if the file can't be read.
    • close

      public void close() throws IOException
      Closes the ZIP file to release resources.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException - if an error occurs while closing the ZIP file.
    • getTotalSize

      public long getTotalSize() throws IOException
      Gets the total size of all files in the ZIP archive.

      This method iterates through all entries in the ZIP and sums their uncompressed sizes.

      Specified by:
      getTotalSize in interface FileAccess
      Returns:
      Total size of all files in bytes (uncompressed)
      Throws:
      IOException - if there's an error accessing the ZIP file
    • getRootPath

      public String getRootPath()
      Description copied from interface: FileAccess
      Retrieves the root path used for file access.
      Specified by:
      getRootPath in interface FileAccess
      Returns:
      The root path used for file access.