Class TCastleZip
Unit
Declaration
type TCastleZip = class(TObject)
Description
A collection of files inside a ZIP archive.
Use this to:
Read ZIP files from any TStream or URL (supported by the Castle Game Engine).
Example of reading:
var Zip: TCastleZip; begin Zip := TCastleZip.Create; try Zip.Open('castle-data:/my_textures.zip'); MyStream := Zip.Read('level1/grass.png'); finally FreeAndNil(Zip) end; end;
Create and write to ZIP files. Using API consistent with above, so we use URLs and slashes for everything and work with all supported platforms and compilers.
Example of writing:
var Zip: TCastleZip; begin Zip := TCastleZip.Create; try Zip.OpenEmpty; Zip.Write('level1/grass.png', 'file:///home/myuser/my_textures/level1/grass.png'); Zip.Save('file:///home/myuser/my_textures.zip'); finally FreeAndNil(Zip) end; end;
The ZIP file is always open in either read-only mode (after Open) or write-only mode (after OpenEmpty).
Right now, we don't support read-write access to the ZIP file, so you cannot modify the ZIP file, e.g. to open an existing ZIP file, add one file (but preserve the rest) and save it. Support for such read-write mode seems to have low priority (as such modification is seldom needed), and uniform support with all compilers would be hard for this.
If you need to modify the ZIP file, for now just recreate it: create one
TCastleZip
instance for reading, iterate over all files using Files, and write them to a newTCastleZip
instance.Optionally register a URL handler, to read or write files inside a ZIP archive by just accesing URL with the given prefix. See RegisterUrlProtocol for example.
Have uniform API for both FPC and Delphi (they have different support in their standard units).
Open a ZIP archive by Open. Later close it by Close. While the ZIP file is open, read files using Read, add files using Write.
Hierarchy
- TObject
- TCastleZip
Overview
Methods
![]() |
constructor Create; |
![]() |
destructor Destroy; override; |
![]() |
procedure BeforeDestruction; override; |
![]() |
procedure Open(const Url: String); overload; |
![]() |
procedure Open(const Stream: TStream; const OwnsStream: boolean); overload; |
![]() |
procedure OpenEmpty; |
![]() |
procedure Close; |
![]() |
function IsOpen: Boolean; |
![]() |
function Read(const PathInZip: String): TStream; |
![]() |
procedure RegisterUrlProtocol(const Protocol: String); |
![]() |
procedure UnregisterUrlProtocol; |
![]() |
procedure Write(const PathInZip: String; const Stream: TStream; const OwnsStream: Boolean); overload; |
![]() |
procedure Write(const PathInZip: String; const Url: String); overload; |
![]() |
procedure Save(const Url: String); |
Properties
![]() |
property Files: TStrings read GetFiles; |
Description
Methods
![]() |
constructor Create; |
This item has no description. |
![]() |
destructor Destroy; override; |
This item has no description. |
![]() |
procedure BeforeDestruction; override; |
This item has no description. |
![]() |
procedure Open(const Url: String); overload; |
Open the ZIP archive from given URL, for reading. Depending on the implementation details, the opened ZIP file may need to exist on disk while we read from it, until we Close it. |
![]() |
procedure OpenEmpty; |
Create a new, empty ZIP archive in memory, for writing. You can then add files to it using Write and save it using Save. |
![]() |
procedure Close; |
Close the ZIP archive. This releases all resources (avoids keeping in memory the ZIP contents), but afterwards you cannot Read or Write files from/to ZIP anymore. There's usually no need to call this, as the destructor or opening a new ZIP archive using Open or OpenEmpty will first close the existing archive. It is only useful if you want to release the resources related to your ZIP earlier, which is in turn only a concern if you really deal with huge (e.g. gigabytes) ZIP files. |
![]() |
function IsOpen: Boolean; |
Was Open or OpenEmpty called and succeded (without any exception) and we didn't yet call Close. |
![]() |
function Read(const PathInZip: String): TStream; |
Read a file from the ZIP archive. PathInZip should be a relative path within the zip archive, with parts separated by slashes, like 'images/my_image.png'. The caller is responsible for freeing the returned stream. The stream is positioned at the beginning, so you can read from it immediately. The stream contents are guaranteed to be valid independently of the TCastleZip instance lifetime and independently of whether you will close the ZIP archive. |
![]() |
procedure RegisterUrlProtocol(const Protocol: String); |
Register handler for a custom URL protocol, to access the ZIP contents by accessing URLs with the given protocol.
For example, you can register a URL handler for 'my-textures' prefix: var Zip: TCastleZip; begin Zip := TCastleZip.Create; try Zip.Open('castle-data:/my_textures.zip'); Zip.RegisterUrlProtocol('my-textures'); MyStream := Download('my-textures:/level1/grass.png'); finally FreeAndNil(Zip) end; end;
Full example usage in the examples/network/custom_url_handler. ZIP doesn't have to be IsOpen when calling this method, |
![]() |
procedure UnregisterUrlProtocol; |
Unregister the URL protocol handler registered by RegisterUrlProtocol. This is automatically done when destroying the TCastleZip instance. |
![]() |
procedure Write(const PathInZip: String; const Stream: TStream; const OwnsStream: Boolean); overload; |
Create a new file entry inside the ZIP. If the given path already existed in the ZIP, it is overwritten. There are 2 overloads, one taking a TStream and one taking a URL of the file to be added.
After this is called, the new file entry appears in the Files. The ZIP archive is not saved to disk until you call Save. If you call Close without calling Save, the changes are lost. Save is never called automatically. |
![]() |
procedure Write(const PathInZip: String; const Url: String); overload; |
This item has no description. |
![]() |
procedure Save(const Url: String); |
Save the currently open ZIP archive (with all modificaiotns done by Write) to the given URL. |
Properties
![]() |
property Files: TStrings read GetFiles; |
Read-only list of all files within the ZIP archive. They are relative paths within the zip archive, with parts separated by slashes. The directories, by itself, are never listed here. Note: This is in contrast to both Delphi TZipFile.FileName and FPC TUnZipper.Entries, where this is undefined, because ZIP files may list directories explicitly, but don't have to. For All these values are valid as arguments for Read. No order of the contents is guaranteed. Exceptions raised
|
Generated by PasDoc 0.16.0-snapshot.