Ruby Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

Ruby
Examples

Quick Start
Unicode
Byte Array
Bz2
Certificates
CSV
Email
Encryption
FTP
HTML Conversion
HTTP
IMAP
MHT
MIME
POP3
RSA
S/MIME
Signatures
SFTP
SMTP
Socket / SSL
Spider
SSH
SSH Key
SSH Tunnel
Tar
HTTP Upload
XML
XMP
Zip

More Examples...
String
Amazon S3
Email Object
DKIM / DomainKey
NTLM
FileAccess
RSS
Atom
Self-Extractor
Service
PPMD
Deflate
Bzip2
DH Key Exchange
DSA
LZW

 

 

 

 

 

 

 

PPMD Streaming String Compression

Demonstrates streaming string compression using PPMD. Data may be compressed in chunks by initially calling one of the BeginCompress* methods, followed by 0 or more calls to a MoreCompress* method, and terminating with a call to an EndCompress* method.

Likewise, data may be decompressed by calling a BeginDecompress* method, followed by 0 or more MoreDecompress* calls, and ending with an EndDecompress* method call.

This example demonstrates string-to-string compression and decompression in chunks.

Downloads for Windows/Linux and Install Instructions

require 'rubygems'
require 'chilkat'

compress = Chilkat::CkCompression.new()

#  Any string argument automatically begins a 30-day trial.
success = compress.UnlockComponent("30-day trial")
if (success != true)
    print "Compression component unlock failed" + "\n"
    exit
end

compress.put_Algorithm("ppmd")
compress.put_Charset("ansi")
compress.put_EncodingMode("base64")

#  Create a long string to compress.
strData = "abcdefg1122334455667788"
strData = strData + "\r\n"

#  Note: BeginCompressStringENC may return a zero-length
#  string.  This is normal if the input is buffered and no
#  compressed data is yet available.
#  However, a null reference indicates an error -- which is
#  generally only possible if the component was never unlocked.
strCompressed = compress.beginCompressStringENC(strData)

#  Compress 100 more chunks of data.  Each call to
#  MoreCompressStringENC may or may not produce additional
#  compressed output.
for i in 0 .. 99
    strData = "abcdefg1122334455667788"
    strData = strData + "\r\n"

    strCompressed = strCompressed + compress.moreCompressStringENC(strData)
end

#  Finalize the compression with a single call to EndCompressStringENC:
strCompressed = strCompressed + compress.endCompressStringENC()

#  Display the compressed string.
#  The result will be:
#  ec8YZ5vk9za18d0plwuV7R65em9OZge9gJXht/QAAAAAAAAAABNOZQAAKw==

print strCompressed + "\n";

#  Decompress back to the original in a single call.

strOriginal = compress.decompressStringENC(strCompressed)

#  Display the uncompressed original:
print "--- Original ---" + "\n";
print strOriginal + "\n";

#  Decompress in multiple calls.  It doesn't matter how the
#  compressed data is split -- it could be fed to the decompressor
#  one char at a time if desired.
chunk1 = "ec8YZ5vk9za1"
chunk2 = "8d0p"
chunk3 = "lwuV7R65em9OZge9gJXht/QAAAA"
chunk4 = "AAAAAABNOZQAAKw=="

strOriginal = ""
strOriginal = compress.beginDecompressStringENC(chunk1)
strOriginal = strOriginal + compress.moreDecompressStringENC(chunk2)
strOriginal = strOriginal + compress.moreDecompressStringENC(chunk3)
strOriginal = strOriginal + compress.moreDecompressStringENC(chunk4)
strOriginal = strOriginal + compress.endDecompressStringENC()

#  Display the uncompressed original after decompressing in chunks:
print "--- Original after Decompressing from Chunks ---" + "\n";
print strOriginal + "\n";
 

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