Sample code for 30+ languages & platforms
Objective-C

Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)

See more Compression Examples

This example demonstrates how to compress text stored in a StringBuilder using the CompressSb method in two ways:

  1. Single-call compression, where the entire input is compressed in one operation.
  2. Chunked compression, where the input is split into multiple parts and processed sequentially using the FirstChunk and LastChunk properties.

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

  • CompressSb converts text to bytes using the Charset property (UTF-8 recommended).
  • Compressed data is appended to a BinData object.
  • When both FirstChunk and LastChunk are true, compression is done in a single call.
  • For chunked processing:
    • First chunk → FirstChunk = true, LastChunk = false
    • Middle chunks → both false
    • Final chunk → LastChunk = true
  • Chunked compression is useful when processing data incrementally.

Chilkat Objective-C Downloads

Objective-C
#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]);