Sample code for 30+ languages & platforms
SQL Server

Compress and Decompress Base64

See more Compression Examples

This example demonstrates how to compress and decompress binary data using the Chilkat.Compression class with the BinData object.

The input data is provided as a Base64-encoded string. It is first decoded into raw bytes and loaded into a BinData object. The example then compresses the data in-place using the deflate algorithm. Because compressed data is binary, it is re-encoded to Base64 for easy display and transport.

To verify correctness, the example performs the reverse operation: it decodes the compressed Base64 back to bytes, decompresses the data, and encodes the result to Base64 again. The final Base64 output is compared with the original input to confirm that the compression and decompression process preserved the data exactly.

This example highlights:

  • Using BinData to hold and transform binary data
  • In-place compression and decompression with CompressBd and DecompressBd
  • Converting between binary data and Base64 for display or transmission
  • Verifying round-trip integrity after compression and decompression

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    -- This is the original data, encoded as base64.
    -- It decodes to plain text containing several repeated lines.
    DECLARE @originalBase64 nvarchar(4000)
    SELECT @originalBase64 = 'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NClRoZSBxdWljayBicm93biBmb3gganVtcGVkIG92ZXIgdGhlIGxhenkgZG9nLg0KVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2cuDQpUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBlZCBvdmVyIHRoZSBsYXp5IGRvZy4NCg0K'

    DECLARE @compress int
    EXEC @hr = sp_OACreate 'Chilkat.Compression', @compress OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @compress, 'Algorithm', 'deflate'

    -- Load the original base64 data into a BinData object.
    -- AppendEncoded decodes the base64 and stores the decoded bytes in binData.
    DECLARE @binData int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @binData OUT

    EXEC sp_OAMethod @binData, 'AppendEncoded', @success OUT, @originalBase64, 'base64'
    IF @success = 0
      BEGIN

        PRINT 'Failed to decode the original base64 data.'
        EXEC @hr = sp_OADestroy @compress
        EXEC @hr = sp_OADestroy @binData
        RETURN
      END

    -- Compress the bytes contained in binData.
    -- CompressBd modifies binData in-place, replacing the original bytes
    -- with the compressed bytes.
    EXEC sp_OAMethod @compress, 'CompressBd', @success OUT, @binData
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @compress
        EXEC @hr = sp_OADestroy @binData
        RETURN
      END

    -- Show the compressed result as base64 so it can be printed as text.
    DECLARE @compressedBase64 nvarchar(4000)
    EXEC sp_OAMethod @binData, 'GetEncoded', @compressedBase64 OUT, 'base64'


    PRINT 'Compressed data, base64 encoded:'

    PRINT @compressedBase64

    -- Expected compressed base64:
    -- C8lIVSgszUzOVkgqyi/PU0jLr1DIKs0tSE1RyC9LLVIoAcrnJFZVKqTkp+vxcoUMYeW8XAA=

    -- --------------------------------------------------------------------
    -- Decompress the data to verify that we get back the original bytes.
    -- --------------------------------------------------------------------

    -- Start with a fresh BinData object containing the compressed bytes.
    -- The compressed data is currently represented as base64 text, so decode it
    -- back to bytes before calling DecompressBd.
    EXEC sp_OAMethod @binData, 'Clear', @success OUT
    EXEC sp_OAMethod @binData, 'AppendEncoded', @success OUT, @compressedBase64, 'base64'
    IF @success = 0
      BEGIN

        PRINT 'Failed to decode the compressed base64 data.'
        EXEC @hr = sp_OADestroy @compress
        EXEC @hr = sp_OADestroy @binData
        RETURN
      END

    -- DecompressBd also modifies binData in-place, replacing the compressed bytes
    -- with the decompressed bytes.
    EXEC sp_OAMethod @compress, 'DecompressBd', @success OUT, @binData
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @compress
        EXEC @hr = sp_OADestroy @binData
        RETURN
      END

    -- Encode the decompressed bytes as base64 so we can compare them with the
    -- original base64 input.
    DECLARE @decompressedBase64 nvarchar(4000)
    EXEC sp_OAMethod @binData, 'GetEncoded', @decompressedBase64 OUT, 'base64'


    PRINT 'Decompressed data, base64 encoded:'

    PRINT @decompressedBase64

    -- The decompressed base64 should match the original base64 input.
    IF @decompressedBase64 = @originalBase64
      BEGIN

        PRINT 'Success. The decompressed data matches the original data.'
      END
    ELSE
      BEGIN

        PRINT 'Failure. The decompressed data does not match the original data.'
      END

    EXEC @hr = sp_OADestroy @compress
    EXEC @hr = sp_OADestroy @binData


END
GO