![]() |
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) Chunked Compression Using CompressBd2 with FirstChunk and LastChunkSee more Compression ExamplesThis example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
#import <CkoCompression.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"; // This will accumulate the compressed output. CkoBinData *bdOut = [[CkoBinData alloc] init]; // ------------------------------------------------------------------ // Simulate input arriving in chunks. // ------------------------------------------------------------------ NSString *part1 = @"The quick brown fox "; NSString *part2 = @"jumps over the lazy dog. "; NSString *part3 = @"This text is split into chunks."; // ------------------------------------------------------------------ // Compress the first chunk // ------------------------------------------------------------------ compress.FirstChunk = YES; compress.LastChunk = NO; CkoBinData *bdIn = [[CkoBinData alloc] init]; [bdIn AppendString: part1 charset: @"utf-8"]; success = [compress CompressBd2: bdIn bdOut: bdOut]; if (success == NO) { NSLog(@"%@",compress.LastErrorText); return; } // ------------------------------------------------------------------ // Compress a middle chunk // ------------------------------------------------------------------ compress.FirstChunk = NO; compress.LastChunk = NO; [bdIn Clear]; [bdIn AppendString: part2 charset: @"utf-8"]; success = [compress CompressBd2: bdIn bdOut: bdOut]; if (success == NO) { NSLog(@"%@",compress.LastErrorText); return; } // ------------------------------------------------------------------ // Compress the final chunk // ------------------------------------------------------------------ compress.FirstChunk = NO; compress.LastChunk = YES; [bdIn Clear]; [bdIn AppendString: part3 charset: @"utf-8"]; success = [compress CompressBd2: bdIn bdOut: bdOut]; if (success == NO) { NSLog(@"%@",compress.LastErrorText); return; } // Get the final compressed result as base64 for display NSString *compressedBase64 = [bdOut GetEncoded: @"base64"]; NSLog(@"%@",@"Compressed data (base64):"); NSLog(@"%@",compressedBase64); // ------------------------------------------------------------------ // Decompress to verify correctness // ------------------------------------------------------------------ CkoBinData *bdDecompressed = [[CkoBinData alloc] init]; compress.FirstChunk = YES; compress.LastChunk = YES; success = [compress DecompressBd2: bdOut bdOut: bdDecompressed]; if (success == NO) { NSLog(@"%@",compress.LastErrorText); return; } // Convert decompressed bytes back to a string NSString *resultText = [bdDecompressed GetString: @"utf-8"]; NSLog(@"%@",@"Decompressed text:"); NSLog(@"%@",resultText); |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.