Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(Excel) 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.
' This example assumes the Chilkat API to have been previously unlocked. ' See Global Unlock Sample for sample code. Dim crypt As Chilkat.Crypt2 Set crypt = Chilkat.NewCrypt2 Dim sb As Chilkat.StringBuilder Set sb = Chilkat.NewStringBuilder i = 0 Do While i < 10 Dim success As Boolean success = sb.AppendInt(i) success = sb.Append(" the quick brown fox jumps over the crazy dog" & vbCrLf) i = i + 1 Loop Debug.Print "Before compression:" Debug.Print 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 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. crypt.Charset = "utf-8" compressedStr = crypt.CompressStringENC(sb.GetAsString()) Debug.Print "After compression:" Debug.Print 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: Debug.Print crypt.ReEncode(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) originalStr = crypt.InflateStringENC(compressedStr) Debug.Print "After inflate:" Debug.Print originalStr ' --------------------------------------------------------------------------- ' To inflate with Chilkat.Compression: Dim compress As Chilkat.Compression Set compress = Chilkat.NewCompression compress.Algorithm = "bzip2" compress.Charset = "utf-8" compress.EncodingMode = "base64" Dim bd As Chilkat.BinData Set bd = Chilkat.NewBinData success = bd.AppendEncoded(compressedStr,"base64") ' Remove the 1st 8 bytes. success = bd.RemoveChunk(0,8) success = compress.DecompressBd(bd) Debug.Print "After decompression:" Debug.Print bd.GetString("utf-8") |
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.