Class ZipArchive

java.lang.Object
VASSAL.tools.io.ZipArchive
All Implemented Interfaces:
Closeable, AutoCloseable, FileArchive

public class ZipArchive extends Object implements FileArchive
Since:
3.2.0
Author:
Joel Uckelman
  • Constructor Details

    • ZipArchive

      public ZipArchive(String path) throws IOException
      Opens a ZIP archive.
      Parameters:
      path - the name of the archive
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(File file) throws IOException
      Opens a ZIP archive.
      Parameters:
      file - the name of the archive
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(String path, boolean truncate) throws IOException
      Opens a ZIP archive.
      Parameters:
      path - the name of the archive
      truncate - if true, truncate the archive file on open
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(File file, boolean truncate) throws IOException
      Opens a ZIP archive.
      Parameters:
      file - the name of the archive
      truncate - if true, truncate the archive file on open
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(Path path, boolean truncate) throws IOException
      Opens a ZIP archive.
      Parameters:
      path - the name of the archive
      truncate - if true, truncate the archive file on open
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(FileArchive src, String dst) throws IOException
      Copies a ZIP archive.
      Parameters:
      src - the name of the source archive
      dst - the name of the destination archive
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(FileArchive src, File dst) throws IOException
      Copies a ZIP archive.
      Parameters:
      src - the name of the source archive
      dst - the name of the destination archive
      Throws:
      IOException - oops
    • ZipArchive

      public ZipArchive(FileArchive src, Path dst) throws IOException
      Copies a ZIP archive.
      Parameters:
      src - the name of the source archive
      dst - the name of the destination archive
      Throws:
      IOException - oops
  • Method Details

    • getName

      public String getName()
      Gets the path to the archive file.
      Specified by:
      getName in interface FileArchive
      Returns:
      the path as a String
    • getFile

      public File getFile()
      Gets the path to the archive file.
      Specified by:
      getFile in interface FileArchive
      Returns:
      the path as a File
    • getPath

      public Path getPath()
    • isClosed

      public boolean isClosed()
      Queries whether the archive is closed.
      Specified by:
      isClosed in interface FileArchive
      Returns:
      true if the archive is closed
    • isModified

      public boolean isModified()
      Queries whether the archive has unsaved modifications.
      Specified by:
      isModified in interface FileArchive
      Returns:
      true if the archive is modified
    • getInputStream

      public InputStream getInputStream(String path) throws IOException
      Gets an InputStream to read from the given file. Note: It is imperative the that calling code ensures that this stream is eventually closed, since the returned stream holds a read lock on the archive.
      Specified by:
      getInputStream in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      an InputStream containing the requested file
      Throws:
      IOException - oops
    • getOutputStream

      public OutputStream getOutputStream(String path) throws IOException
      Gets an OutputStream to write to the given file. Note: It is imperative the that calling code ensures that this stream is eventually closed, since the returned stream holds a write lock on the archive.
      Specified by:
      getOutputStream in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      an OutputStream for the requested file
      Throws:
      IOException - oops
    • getOutputStream

      public OutputStream getOutputStream(String path, boolean compress) throws IOException
      Gets an OutputStream to write to the given file. Note: It is imperative the that calling code ensures that this stream is eventually closed, since the returned stream holds a write lock on the archive.
      Parameters:
      path - the path to the file in the archive
      compress - whether to compress the file
      Returns:
      an OutputStream for the requested file
      Throws:
      IOException - oops
    • add

      public void add(String path, String extPath) throws IOException
      Adds a file to the archive.
      Specified by:
      add in interface FileArchive
      Parameters:
      path - the internal path of the file to be added
      extPath - the external path of the file to be added
      Throws:
      IOException - oops
    • add

      public void add(String path, File extPath) throws IOException
      Adds a file to the archive.
      Specified by:
      add in interface FileArchive
      Parameters:
      path - the internal path of the file to be added
      extPath - the external path to the file to be added
      Throws:
      IOException - oops
    • add

      public void add(String path, byte[] bytes) throws IOException
      Adds the contents of a byte array to the archive.
      Specified by:
      add in interface FileArchive
      Parameters:
      path - the internal path of the file to be added
      bytes - the bytes to be added
      Throws:
      IOException - oops
    • add

      public void add(String path, InputStream in) throws IOException
      Adds the contents of an InputStream to the archive.
      Specified by:
      add in interface FileArchive
      Parameters:
      path - the internal path of the file to be added
      in - the InputStream to read from
      Throws:
      IOException - oops
    • remove

      public boolean remove(String path) throws IOException
      Removes a file from the archive.
      Specified by:
      remove in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      true if the file existed in the archive
      Throws:
      IOException - oops
    • revert

      public void revert() throws IOException
      Reverts the archive to its last saved state.
      Specified by:
      revert in interface FileArchive
      Throws:
      IOException - oops
    • flush

      public void flush() throws IOException
      Forces all changes to the archive to disk.
      Specified by:
      flush in interface FileArchive
      Throws:
      IOException - oops
    • close

      public void close() throws IOException
      Closes the archive, writing all changes to disk.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface FileArchive
      Throws:
      IOException - oops
    • contains

      public boolean contains(String path) throws IOException
      Queries whether a file exists in the archive.
      Specified by:
      contains in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      true if the file exists in the archive
      Throws:
      IOException - oops
    • getSize

      public long getSize(String path) throws IOException
      Gets the size of a file in the archive, in bytes.
      Specified by:
      getSize in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      the size of the file, in bytes
      Throws:
      IOException - oops
    • getCompressedSize

      public long getCompressedSize(String path) throws IOException
      Get the compressed size of a file in the archive, in bytes. Rerurn the uncompressed size by default.
      Specified by:
      getCompressedSize in interface FileArchive
      Returns:
      the compressed size of the file, in bytes
      Throws:
      IOException - oops
    • getMTime

      public long getMTime(String path) throws IOException
      Gets the modification time of a file in the archive, in milliseconds since the epoch.
      Specified by:
      getMTime in interface FileArchive
      Parameters:
      path - the path to the file in the archive
      Returns:
      the mtime of the file
      Throws:
      IOException - oops
    • getFiles

      public List<String> getFiles() throws IOException
      Gets the list of files in the archive.
      Specified by:
      getFiles in interface FileArchive
      Returns:
      the list of files in the archive
      Throws:
      IOException - oops
    • getFiles

      public List<String> getFiles(String root) throws IOException
      Gets the list of files under a given directory of the archive.
      Specified by:
      getFiles in interface FileArchive
      Parameters:
      root - the directory
      Returns:
      the list of files under the given directory
      Throws:
      IOException - oops