Package VASSAL.tools.io
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 Summary
Constructors Constructor Description ZipArchive(File file)
Opens a ZIP archive.ZipArchive(File file, boolean truncate)
Opens a ZIP archive.ZipArchive(String path)
Opens a ZIP archive.ZipArchive(String path, boolean truncate)
Opens a ZIP archive.ZipArchive(FileArchive src, File dst)
Copies a ZIP archive.ZipArchive(FileArchive src, String dst)
Copies a ZIP archive. -
Method Summary
Modifier and Type Method Description void
add(String path, byte[] bytes)
Adds the contents of a byte array to the archive.void
add(String path, File extPath)
Adds a file to the archive.void
add(String path, InputStream in)
Adds the contents of anInputStream
to the archive.void
add(String path, String extPath)
Adds a file to the archive.void
close()
Closes the archive, writing all changes to disk.boolean
contains(String path)
Queries whether a file exists in the archive.void
flush()
Forces all changes to the archive to disk.File
getFile()
Gets the path to the archive file.List<String>
getFiles()
Gets the list of files in the archive.List<String>
getFiles(String root)
Gets the list of files under a given directory of the archive.InputStream
getInputStream(String path)
Gets anInputStream
to read from the given file.long
getMTime(String path)
Gets the modification time of a file in the archive, in milliseconds since the epoch.String
getName()
Gets the path to the archive file.OutputStream
getOutputStream(String path)
Gets anOutputStream
to write to the given file.OutputStream
getOutputStream(String path, boolean compress)
Gets anOutputStream
to write to the given file.long
getSize(String path)
Gets the size of a file in the archive, in bytes.boolean
isClosed()
Queries whether the archive is closed.boolean
isModified()
Queries whether the archive has unsaved modifications.static void
main(String[] args)
boolean
remove(String path)
Removes a file from the archive.void
revert()
Reverts the archive to its last saved state.
-
Constructor Details
-
ZipArchive
Opens a ZIP archive.- Parameters:
path
- the name of the archive- Throws:
IOException
-
ZipArchive
Opens a ZIP archive.- Parameters:
file
- the name of the archive- Throws:
IOException
-
ZipArchive
Opens a ZIP archive.- Parameters:
path
- the name of the archivetruncate
- iftrue
, truncate the archive file on open- Throws:
IOException
-
ZipArchive
Opens a ZIP archive.- Parameters:
file
- the name of the archivetruncate
- iftrue
, truncate the archive file on open- Throws:
IOException
-
ZipArchive
Copies a ZIP archive.- Parameters:
src
- the name of the source archivedst
- the name of the destination archive- Throws:
IOException
-
ZipArchive
Copies a ZIP archive.- Parameters:
src
- the name of the source archivedst
- the name of the destination archive- Throws:
IOException
-
-
Method Details
-
getName
Gets the path to the archive file.- Specified by:
getName
in interfaceFileArchive
- Returns:
- the path as a
String
-
getFile
Gets the path to the archive file.- Specified by:
getFile
in interfaceFileArchive
- Returns:
- the path as a
File
-
isClosed
public boolean isClosed()Queries whether the archive is closed.- Specified by:
isClosed
in interfaceFileArchive
- Returns:
true
if the archive is closed
-
isModified
public boolean isModified()Queries whether the archive has unsaved modifications.- Specified by:
isModified
in interfaceFileArchive
- Returns:
true
if the archive is modified
-
getInputStream
Gets anInputStream
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 interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
- an
InputStream
containing the requested file - Throws:
IOException
-
getOutputStream
Gets anOutputStream
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 interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
- an
OutputStream
for the requested file - Throws:
IOException
-
getOutputStream
Gets anOutputStream
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 archivecompress
- whether to compress the file- Returns:
- an
OutputStream
for the requested file - Throws:
IOException
-
add
Adds a file to the archive.- Specified by:
add
in interfaceFileArchive
- Parameters:
path
- the internal path of the file to be addedextPath
- the external path of the file to be added- Throws:
IOException
-
add
Adds a file to the archive.- Specified by:
add
in interfaceFileArchive
- Parameters:
path
- the internal path of the file to be addedextPath
- the external path to the file to be added- Throws:
IOException
-
add
Adds the contents of a byte array to the archive.- Specified by:
add
in interfaceFileArchive
- Parameters:
path
- the internal path of the file to be addedbytes
- the bytes to be added- Throws:
IOException
-
add
Adds the contents of anInputStream
to the archive.- Specified by:
add
in interfaceFileArchive
- Parameters:
path
- the internal path of the file to be added- Throws:
IOException
-
remove
Removes a file from the archive.- Specified by:
remove
in interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
true
if the file existed in the archive- Throws:
IOException
-
revert
Reverts the archive to its last saved state.- Specified by:
revert
in interfaceFileArchive
- Throws:
IOException
-
flush
Forces all changes to the archive to disk.- Specified by:
flush
in interfaceFileArchive
- Throws:
IOException
-
close
Closes the archive, writing all changes to disk.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceFileArchive
- Throws:
IOException
-
contains
Queries whether a file exists in the archive.- Specified by:
contains
in interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
true
if the file exists in the archive- Throws:
IOException
-
getSize
Gets the size of a file in the archive, in bytes.- Specified by:
getSize
in interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
- the size of the file, in bytes
- Throws:
FileNotFoundException
- ifpath
is not in the archiveIOException
-
getMTime
Gets the modification time of a file in the archive, in milliseconds since the epoch.- Specified by:
getMTime
in interfaceFileArchive
- Parameters:
path
- the path to the file in the archive- Returns:
- the mtime of the file
- Throws:
FileNotFoundException
- ifpath
is not in the archiveIOException
-
getFiles
Gets the list of files in the archive.- Specified by:
getFiles
in interfaceFileArchive
- Returns:
- the list of files in the archive
- Throws:
IOException
-
getFiles
Gets the list of files under a given directory of the archive.- Specified by:
getFiles
in interfaceFileArchive
- Parameters:
root
- the directory- Returns:
- the list of files under the given directory
- Throws:
FileNotFoundException
- ifroot
is not in the archiveIOException
-
main
- Throws:
IOException
-