Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Ruby Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Ruby) 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 Ruby Downloads

install from rubygems.org

gem install chilkat

or download... Ruby Library for Windows, MacOS, Linux, Alpine Linux

require 'chilkat'

success = false

# This example assumes the Chilkat API has already been unlocked.
# See Global Unlock Sample for sample code.

compress = Chilkat::CkCompression.new()
compress.put_Algorithm("zlib")

# ================================================================
# 1) Single-call compression (entire data in one call)
# ================================================================

sb = Chilkat::CkStringBuilder.new()
sb.Append("The quick brown fox jumps over the lazy dog. ")
sb.Append("This is a simple example using CompressSb.")

bdCompressed = Chilkat::CkBinData.new()

# When both FirstChunk and LastChunk are true (the defaults),
# the entire compression happens in a single call.
compress.put_FirstChunk(true)
compress.put_LastChunk(true)

success = compress.CompressSb(sb,bdCompressed)
if (success == false)
    print compress.lastErrorText() + "\n";
    exit
end

compressedBase64 = bdCompressed.getEncoded("base64")

print "Single-call compressed (base64):" + "\n";
print compressedBase64 + "\n";

# ================================================================
# 2) Chunked compression using FirstChunk / LastChunk
# ================================================================

bdChunkedOut = Chilkat::CkBinData.new()

# First chunk
compress.put_FirstChunk(true)
compress.put_LastChunk(false)

sbPart = Chilkat::CkStringBuilder.new()
sbPart.Append("The quick brown fox ")

success = compress.CompressSb(sbPart,bdChunkedOut)
if (success == false)
    print compress.lastErrorText() + "\n";
    exit
end

# Middle chunk
compress.put_FirstChunk(false)
compress.put_LastChunk(false)

sbPart.Clear()
sbPart.Append("jumps over the lazy dog. ")

success = compress.CompressSb(sbPart,bdChunkedOut)
if (success == false)
    print compress.lastErrorText() + "\n";
    exit
end

# Final chunk
compress.put_FirstChunk(false)
compress.put_LastChunk(true)

sbPart.Clear()
sbPart.Append("This is a chunked CompressSb example.")

success = compress.CompressSb(sbPart,bdChunkedOut)
if (success == false)
    print compress.lastErrorText() + "\n";
    exit
end

chunkedBase64 = bdChunkedOut.getEncoded("base64")

print "Chunked compressed (base64):" + "\n";
print chunkedBase64 + "\n";

# ================================================================
# 3) Decompress to verify correctness
# ================================================================

sbDecompressed = Chilkat::CkStringBuilder.new()

# Decompress in a single call (entire data already assembled)
compress.put_FirstChunk(true)
compress.put_LastChunk(true)

success = compress.DecompressSb(bdChunkedOut,sbDecompressed)
if (success == false)
    print compress.lastErrorText() + "\n";
    exit
end

print "Decompressed text:" + "\n";
print sbDecompressed.getAsString() + "\n";

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.