![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Objective-C) Compressing StringBuilder Data Using CompressSb (Single Call and Chunked)See more Compression ExamplesThis example demonstrates how to compress text stored in aStringBuilder using the CompressSb method in two ways:
The example shows how compressed output is appended to a Key Points
#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]); |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.