Objective-C
Objective-C
Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)
See more Compression Examples
This example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
- Single-call compression, where the entire input is compressed in one operation.
- Chunked compression, where the input is split into multiple parts and processed sequentially using the
FirstChunkandLastChunkproperties.
The example shows how compressed output is appended to a BinData object, and how the result can be verified by decompressing back to the original text. This is useful for both simple use cases and scenarios where data is processed incrementally (such as streaming or large inputs).
Key Points
CompressSbconverts text to bytes using theCharsetproperty (UTF-8 recommended).- Compressed data is appended to a
BinDataobject. - When both
FirstChunkandLastChunkaretrue, compression is done in a single call. - For chunked processing:
- First chunk →
FirstChunk = true,LastChunk = false - Middle chunks → both
false - Final chunk →
LastChunk = true
- First chunk →
- Chunked compression is useful when processing data incrementally.
Chilkat Objective-C Downloads
#import <CkoCompression.h>
#import <CkoStringBuilder.h>
#import <CkoBinData.h>
#import <NSString.h>
BOOL success = NO;
// This example assumes the Chilkat API has already been unlocked.
// See Global Unlock Sample for sample code.
CkoCompression *compress = [[CkoCompression alloc] init];
compress.Algorithm = @"zlib";
// ================================================================
// 1) Single-call compression (entire data in one call)
// ================================================================
CkoStringBuilder *sb = [[CkoStringBuilder alloc] init];
[sb Append: @"The quick brown fox jumps over the lazy dog. "];
[sb Append: @"This is a simple example using CompressSb."];
CkoBinData *bdCompressed = [[CkoBinData alloc] init];
// When both FirstChunk and LastChunk are true (the defaults),
// the entire compression happens in a single call.
compress.FirstChunk = YES;
compress.LastChunk = YES;
success = [compress CompressSb: sb binData: bdCompressed];
if (success == NO) {
NSLog(@"%@",compress.LastErrorText);
return;
}
NSString *compressedBase64 = [bdCompressed GetEncoded: @"base64"];
NSLog(@"%@",@"Single-call compressed (base64):");
NSLog(@"%@",compressedBase64);
// ================================================================
// 2) Chunked compression using FirstChunk / LastChunk
// ================================================================
CkoBinData *bdChunkedOut = [[CkoBinData alloc] init];
// First chunk
compress.FirstChunk = YES;
compress.LastChunk = NO;
CkoStringBuilder *sbPart = [[CkoStringBuilder alloc] init];
[sbPart Append: @"The quick brown fox "];
success = [compress CompressSb: sbPart binData: bdChunkedOut];
if (success == NO) {
NSLog(@"%@",compress.LastErrorText);
return;
}
// Middle chunk
compress.FirstChunk = NO;
compress.LastChunk = NO;
[sbPart Clear];
[sbPart Append: @"jumps over the lazy dog. "];
success = [compress CompressSb: sbPart binData: bdChunkedOut];
if (success == NO) {
NSLog(@"%@",compress.LastErrorText);
return;
}
// Final chunk
compress.FirstChunk = NO;
compress.LastChunk = YES;
[sbPart Clear];
[sbPart Append: @"This is a chunked CompressSb example."];
success = [compress CompressSb: sbPart binData: bdChunkedOut];
if (success == NO) {
NSLog(@"%@",compress.LastErrorText);
return;
}
NSString *chunkedBase64 = [bdChunkedOut GetEncoded: @"base64"];
NSLog(@"%@",@"Chunked compressed (base64):");
NSLog(@"%@",chunkedBase64);
// ================================================================
// 3) Decompress to verify correctness
// ================================================================
CkoStringBuilder *sbDecompressed = [[CkoStringBuilder alloc] init];
// Decompress in a single call (entire data already assembled)
compress.FirstChunk = YES;
compress.LastChunk = YES;
success = [compress DecompressSb: bdChunkedOut sb: sbDecompressed];
if (success == NO) {
NSLog(@"%@",compress.LastErrorText);
return;
}
NSLog(@"%@",@"Decompressed text:");
NSLog(@"%@",[sbDecompressed GetAsString]);