Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Crypt2 Compression ExampleThe Chilkat Crypt2 class include a few methods for compression and decompression (inflate). These are legacy method that existed long before the Chilkat.Compression class was first introduced. These methods should be avoided in favor of using the Chilkat.Compression methods. The reason is that they are limited to bzip2, and the compressed output includes an 8-byte header that is composed of a magic number (0xB394A7E1) and the size of the original uncompressed data (4 bytes and thus limited to 4GB). This example demonstrates compression and decompression, and also how to compress using Crypt2 and decompress using Chilkat.Compression. The Chilkat.Compression class does not emit any header, and supports other compression methods.
integer li_rc oleobject loo_Crypt oleobject loo_Sb integer i string ls_CompressedStr string ls_OriginalStr oleobject loo_Compress oleobject loo_Bd // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Crypt = create oleobject // Use "Chilkat_9_5_0.Crypt2" for versions of Chilkat < 10.0.0 li_rc = loo_Crypt.ConnectToNewObject("Chilkat.Crypt2") if li_rc < 0 then destroy loo_Crypt MessageBox("Error","Connecting to COM object failed") return end if loo_Sb = create oleobject // Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0 li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder") i = 0 do while i < 10 loo_Sb.AppendInt(i) loo_Sb.Append(" the quick brown fox jumps over the crazy dog~r~n") i = i + 1 loop Write-Debug "Before compression:" Write-Debug loo_Sb.GetAsString() // Before compression: // 0 the quick brown fox jumps over the crazy dog // 1 the quick brown fox jumps over the crazy dog // 2 the quick brown fox jumps over the crazy dog // 3 the quick brown fox jumps over the crazy dog // 4 the quick brown fox jumps over the crazy dog // 5 the quick brown fox jumps over the crazy dog // 6 the quick brown fox jumps over the crazy dog // 7 the quick brown fox jumps over the crazy dog // 8 the quick brown fox jumps over the crazy dog // 9 the quick brown fox jumps over the crazy dog loo_Crypt.EncodingMode = "base64" // Compress the utf-8 representation of the string. // Note: Choosing "Unicode" or "utf-16" is a poor choice for text that is mostly us-ascii. // The utf-8 representation will result in a smaller size. loo_Crypt.Charset = "utf-8" ls_CompressedStr = loo_Crypt.CompressStringENC(loo_Sb.GetAsString()) Write-Debug "After compression:" Write-Debug ls_CompressedStr // After compression: // 4aeUs+ABAABCWmgzMUFZJlNZT/+H9wAAN1mAABJAAH/gP/v/8CAAkDGExNBgjEMjCYFKqGoAaDygNDamyTYmKe6Ypqn0mqZpgmaZd/hv8fLz9McsU7kyTgmwnAmibU4pyJom5PxNqapxTBO1M05JuT4TrT+TRMCapqm9VzTmn+LuSKcKEgn/8P7g // Note: The compressed data includes an 8-byte header composed of a magic number (0xB394A7E1) and the original data byte count. // Therefore, the Crypt2 compress/inflate methods are considered legacy and should only be used for data already // compressed with these methods. The Chilkat.Compression class is generalized, emits no headers, and can interoperate with // with other software systems without trouble. // If we re-encode from base64 to hex, then we can see the header: Write-Debug loo_Crypt.ReEncode(ls_CompressedStr,"base64","hex") // The hex compressed string is: // E1A794B3E0010000425A68333141592653594... // We can see the 8-byte header in the hex: magic number = 0xE1A794B3E (little-endian), and byte count = 0xE0010000 (little-endian) ls_OriginalStr = loo_Crypt.InflateStringENC(ls_CompressedStr) Write-Debug "After inflate:" Write-Debug ls_OriginalStr // --------------------------------------------------------------------------- // To inflate with Chilkat.Compression: loo_Compress = create oleobject // Use "Chilkat_9_5_0.Compression" for versions of Chilkat < 10.0.0 li_rc = loo_Compress.ConnectToNewObject("Chilkat.Compression") loo_Compress.Algorithm = "bzip2" loo_Compress.Charset = "utf-8" loo_Compress.EncodingMode = "base64" loo_Bd = create oleobject // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData") loo_Bd.AppendEncoded(ls_CompressedStr,"base64") // Remove the 1st 8 bytes. loo_Bd.RemoveChunk(0,8) loo_Compress.DecompressBd(loo_Bd) Write-Debug "After decompression:" Write-Debug loo_Bd.GetString("utf-8") destroy loo_Crypt destroy loo_Sb destroy loo_Compress destroy loo_Bd |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.