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
    • ZipArchive

      public ZipArchive​(File file) throws IOException
      Opens a ZIP archive.
      Parameters:
      file - the name of the archive
      Throws:
      IOException
    • 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
    • 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
    • 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
    • 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
  • 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
    • 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 impeative 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
      Throws:
      IOException
    • 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
    • revert

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

      public void flush() throws IOException
      Forces all changes to the archive to disk.
      Specified by:
      flush in interface FileArchive
      Throws:
      IOException
    • 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
    • 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
    • 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:
      FileNotFoundException - if path is not in the archive
      IOException
    • 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:
      FileNotFoundException - if path is not in the archive
      IOException
    • 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
    • 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:
      FileNotFoundException - if root is not in the archive
      IOException
    • main

      public static void main​(String[] args) throws IOException
      Throws:
      IOException