![]() |
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
(Swift) 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
func chilkatTest() { var success: Bool = false // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for sample code. let compress = CkoCompression()! compress.algorithm = "zlib" // ================================================================ // 1) Single-call compression (entire data in one call) // ================================================================ let sb = CkoStringBuilder()! sb.append("The quick brown fox jumps over the lazy dog. ") sb.append("This is a simple example using CompressSb.") let bdCompressed = CkoBinData()! // When both FirstChunk and LastChunk are true (the defaults), // the entire compression happens in a single call. compress.firstChunk = true compress.lastChunk = true success = compress.compressSb(sb, binData: bdCompressed) if success == false { print("\(compress.lastErrorText!)") return } var compressedBase64: String? = bdCompressed.getEncoded("base64") print("Single-call compressed (base64):") print("\(compressedBase64!)") // ================================================================ // 2) Chunked compression using FirstChunk / LastChunk // ================================================================ let bdChunkedOut = CkoBinData()! // First chunk compress.firstChunk = true compress.lastChunk = false let sbPart = CkoStringBuilder()! sbPart.append("The quick brown fox ") success = compress.compressSb(sbPart, binData: bdChunkedOut) if success == false { print("\(compress.lastErrorText!)") return } // Middle chunk compress.firstChunk = false compress.lastChunk = false sbPart.clear() sbPart.append("jumps over the lazy dog. ") success = compress.compressSb(sbPart, binData: bdChunkedOut) if success == false { print("\(compress.lastErrorText!)") return } // Final chunk compress.firstChunk = false compress.lastChunk = true sbPart.clear() sbPart.append("This is a chunked CompressSb example.") success = compress.compressSb(sbPart, binData: bdChunkedOut) if success == false { print("\(compress.lastErrorText!)") return } var chunkedBase64: String? = bdChunkedOut.getEncoded("base64") print("Chunked compressed (base64):") print("\(chunkedBase64!)") // ================================================================ // 3) Decompress to verify correctness // ================================================================ let sbDecompressed = CkoStringBuilder()! // Decompress in a single call (entire data already assembled) compress.firstChunk = true compress.lastChunk = true success = compress.decompressSb(bdChunkedOut, sb: sbDecompressed) if success == false { print("\(compress.lastErrorText!)") return } print("Decompressed text:") print("\(sbDecompressed.getAsString()!)") } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.