GZipCompress
Function GZipCompress( const Data : TBytes) : TBytes
Example
procedure ScriptEvent(var Value: variant);
var
Data: TBytes;
begin
Data := Base64DecodeBytes('RXhhbXBsZSBwYXlsb2Fk');
Value := Base64EncodeBytes(GZipCompress(Data));
end;
Usage
Use GZipCompress to convert a TBytes value into a new gzip-compressed byte array. It works with bytes, not text; encode text deliberately before compression and preserve the corresponding decoding contract for the receiver.
Parameters
| Name | Type | Description |
|---|---|---|
Data | TBytes, const | Complete uncompressed byte array. The function does not interpret text or add an application-level filename. |
Returns
A new byte array containing one gzip member when compression completes successfully.
Errors
Allocation failures and array/range failures propagate. On a caught exception the function clears its result and re-raises.
Usage notes
Use GZipCompressString only when the intended payload is Velox UTF-16LE text. For arbitrary bytes, this function is the appropriate gzip entry, subject to the current growth and empty-input defects. Base64 is a transport representation and is not part of the gzip format.
Additional Technical Info
GZipCompress attempts to encode the complete TBytes value as one gzip member at Delphi's default compression level. It is a Velox-owned adaptation of System.ZLib.ZCompress, changed to call DeflateInit2 with window bits 31 so zlib emits a gzip header and trailer.
The example converts fictional Base64 text to bytes, compresses those bytes and converts the binary result back to Base64 for transport. It is source-reviewed and is not executed by the documentation workflow.
Implementation
The PascalScript import registers vxCompression.GZipCompress directly. The implementation:
- calculates an initial output allocation as the input length plus 10%, plus 12 bytes, rounded up to a 256-byte boundary;
- points a zero-initialised
TZStreamRecat the input and result arrays; - calls
System.ZLib.DeflateInit2with the default zlib level, deflate method, window bits31, memory level8and default strategy; - calls
deflate(..., Z_FINISH)untilZ_STREAM_END; and - calls
deflateEndand shrinks the result tototal_out.
Window bits 31 means a 15-bit deflate window plus gzip wrapper generation. The result is gzip data, not a ZIP archive or zlib-wrapped stream.
Edge cases and quirks
- For an empty input, the initial output length is zero. The implementation immediately evaluates
@Data[0]; current Velox project configurations enable range checking, so the call raises a range-check exception before compression. A build without range checks would reach the separate zero-output-space/zlib-buffer-error boundary. Do not pass an empty array. - The buffer-growth branch contains a source defect: after resizing,
next_outis reset to@Result[0]instead of the currenttotal_outoffset. If that branch is reached, later output can overwrite the beginning of the gzip member and produce corrupt data. The normal initial allocation is deliberately generous, but the defect remains a correctness boundary for unusually large or overflow-affected inputs. - Input and output sizes are held in 32-bit
Integerfields and zlib counters. This is not a large-file or streaming interface. - A gzip member created here carries no original filename, application metadata, encryption or authentication.
Side effects
No external side effect. The function allocates result and zlib working memory.
Performance and concurrency
Compression is synchronous and generally proportional to input size and compressibility. The entire input and complete result are simultaneously resident, in addition to zlib working memory. State is local to the call; no shared mutable compression context is used.
Related entries
GZipDecompressis the intended byte-array inverse.GZipCompressFilestreams a file into a gzip result.ZLibCompresscreates a zlib-wrapped stream instead.Zipcreates a named multi-entry ZIP archive.
External references
- Embarcadero
System.ZLib- the current page exposes the low-leveldeflateInit2/deflateAPIs used by the wrapper. - Free Pascal
ZStream- compatibility context only; it does not define Velox's custom buffer logic.