Archive#
This class represents a generalization of file folders and container files like ZIP and TAR archives. Archives allow accessing arbitrary collections of file folders, ZIP / TAR files and single binary data elements as if they all were part of one hierarchical tree of folders.
Archives are currently only used by Story objects to specify where to look for fonts, images and other resources.
Method / Attribute |
Short Description |
|---|---|
Add new data to the archive |
|
Check if given name is a member |
|
Read the data given by the name |
|
List of SubArchive items |
Class API
- class Archive#
- Archive(string content, string path: "")#
Creates a new archive. Without parameters, an empty archive is created.
If provided,
contentmay be one of the following:another Archive: the archive is being made a sub-archive of the new one.
a string: this must be the name of a local folder or file.
A folder will be converted to a sub-archive, so its files (and any sub-folders) can be accessed by their names.
A file will be read with mode
"rb"and these binary data be treated as a single-member sub-archive. In this case, thepathparameter is mandatory and should be the member name under which this item can be found / retrieved.
a binary object: this will add a single-member sub-archive. In this case, the
pathparameter is mandatory and should be the member name under which this item can be found / retrieved.a tuple
(data, name): This will add a single-member sub-archive with the member namename.datamay be a binary object or a local file name (in which case its binary file content is used). Use this format if you need to specifypath.a sequence: This is a convenience format to specify any combination of the above.
If provided,
pathmust be a string.If
contentis either binary data or a file name, this parameter is mandatory and must be the name under which the data can be found.Otherwise this parameter is optional. It can be used to simulate a folder name or a mount point, under which this sub-archive’s elements can be found. For example this specification
Archive((data, "name"), "path")means thatdatawill be found using the element name"path/name". Similar is true for other sub-archives: to retrieve members of a ZIP sub-archive, their names must be prefixed with"path/". The main purpose of this parameter probably is to differentiate between duplicate names.
Note
If duplicate entry names exist in the archive, always the last entry with that name will be found / retrieved. During archive creation, or appending more data to an archive (see
Archive.add()) no check for duplicates will be made. Use thepathparameter to prevent this from happening.
- Add(string content, string path)#
- Add(ZipArchive content, string path)#
- Add(TarReader content, string path)#
- Add(FzArchive content, string path)#
- Add(byte[] content, string path)#
Append a sub-archive. The meaning of the parameters are exactly the same as explained above. Of course, parameter
contentis not optional here.
- HasEntry(string name)#
Checks whether an entry exists in any of the sub-archives.
- Parameters:
name (string) – The fully qualified name of the entry. So must include any
pathprefix under which the entry’s sub-archive has been added.- Returns:
trueorfalse.
- ReadEntry(string name)#
Retrieve the data of an entry.
- Parameters:
name (string) – The fully qualified name of the entry. So must include any
pathprefix under which the entry’s sub-archive has been added.- Returns:
The binary data (
bytes) of the entry. If not found, an exception is raised.
- EntryList#
A list of the archive’s sub-archives. Each list item is a dictionary with the following keys:
entries– a list of (top-level) entry names in this sub-archive.fmt– the format of the sub-archive. This is one of the strings “dir” (file folder), “zip” (ZIP archive), “tar” (TAR archive), or “tree” for single binary entries or file content.path– the value of thepathparameter under which this sub-archive was added.
