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 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;
Modify and write ZIP files, consisting with above.
TODO: For now, with FPC, ZIP can only be open for reading or writing (when OpenEmpty). So you cannot open existing ZIP and then modify it.
TODO: Delphi version doesn't support writing yet, at all.
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;
Optionally register a URL handler, to read files inside a ZIP archive by just accesing URL with the given prefix. See RegisterUrlProtocol for example.
TODO: add write support this way too. For now, it only works for reading, when the ZIP was opened for reading.
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 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); |
Description
Methods
![]() |
constructor Create; |
This item has no description. |
![]() |
destructor Destroy; override; |
This item has no description. |
![]() |
procedure Open(const Url: String); overload; |
Open the ZIP archive from given URL. 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. 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 files from ZIP anymore. There's usually no need to call this, as the destructor or opening a new ZIP archive using Open 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 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 read handler for a given URL protocol, to access the ZIP contents by just reading from a given URL protocol using Download routine (or any routine on top of it). 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. In fact, you can close and reopen the ZIP file while the URL handler continues to be registered. But it has to be open when you actually read using the indicated URL protocol. |
![]() |
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 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. |
Generated by PasDoc 0.16.0-snapshot.