Table of Contents
An archive is a collection of files and directories, stored as archive entries in a single file. The archive often also stores some kind of metadata for each entry, such as its owner or its latest modification time.
At4J supports reading and creating Zip and Tar archives.
A program can access files and directories in an archive by creating an Archive object, such as ZipFile or TarFile on the archive file. The archive contains a set of ArchiveEntry:s containing data on each archive entry, each with its unique AbsoluteLocation in the archive. Entries may be ArchiveFileEntry:s, ArchiveDirectoryEntry:s or ArchiveSymbolicLinkEntry:s. An ArchiveFileEntry is a ReadableFile, and an ArchiveDirectoryEntry has a map of child entries with their names as keys. Archive objects implement Map<AbsoluteLocation, ArchiveEntry>, (read only) which makes it easy to access individual entries.
Entries in Zip and Tar archives differ in what kind of metadata they store, so each archive format has its own ArchiveEntry implementations. See Chapter 8, Tar and Chapter 9, Zip for examples.
Since an Archive object keeps the backing archive file open, it must be closed when the program is done using it.
Entries can be extracted and copied to a FileSystem by manually traversing through the entries in an Archive object or by using the ArchiveExtractor.
Example 7.1. Extracting files from a Zip file using the archive extractor
// Extract all entries from the Zip file f to the directory d ZipFile zf = new ZipFile(f); try { // Extract to d new ArchiveExtractor(zf).extract(d); } finally { // Close the Zip file to release all its resources zf.close(); }
The ArchiveExtractor's extraction process can be fine-tuned by giving it a custom ExtractSpecification object.
For some archive formats (Tar), there are customized archive extractors (TarExtractor) that may be faster than the ArchiveExtractor.
An archive file is created by creating a new archive format-specific ArchiveBuilder object, for instance a ZipBuilder, and then adding entries to it. See the following chapters for examples.
An archive builder may or may not be a StreamAddCapableArchiveBuilder. A stream add capable builder has methods for adding data read from an input stream as a file entry in the archive.
The metadata added to each entry is determined by its effective ArchiveEntrySettings object (ZipEntrySettings, TarEntrySettings, etc). Entry settings can be defined in three different scopes:
The archive builder arrives at the effective settings for each entry by:
| 1. | Combine the default file or directory settings with the settings from the first applicable global rule. |
| 2. | Combine the settings created by the previous step with the settings from the second applicable global rule. |
3 – n - 1. | … |
| n. | Combine the settings created by the previous step with the settings from the last applicable global rule. |
n + 1. | Combine the settings created by the previous step with the settings from the first applicable rule for the add operation. |
n + 2 – n + m - 1. | … |
n + m. | Combine the settings created by the previous step with the settings from the last applicable rule for the add operation. |
When combining settings object A with settings object B, a new settings object C is created that contains the values of properties from A, overridden by the values for the properties that are are set in B.