Compression SQL Server Reference Documentation

Compression

Current Version: 11.6.0

Chilkat.Compression

Compress and decompress bytes, text, files, and streams with a flexible API.

Chilkat.Compression provides a general-purpose API for compressing and decompressing data in memory, files, and streams. It supports multiple algorithms, text charset conversion, encoded output, chunked processing, and streaming operations for large data.

Multiple algorithms

Supports deflate, zlib, bzip2, and lzw.

Flexible data types

Work with byte arrays, strings, BinData, StringBuilder, files, and streams.

Chunked processing

Process data incrementally with FirstChunk and LastChunk for chunk-aware methods.

Encoded output

Return compressed bytes as text using encodings such as base64, hex, and others.

Streaming support

Compress or decompress large files and streams with stable memory usage.

Compression + encryption

Combine compression and encryption in file workflows with integrated methods.

Tip: For text compression, explicitly set Charset = "utf-8" unless another encoding is required. For encoded compressed data, set EncodingMode to the desired output format.

Object Creation

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

-- ... use @compression ...

EXEC @hr = sp_OADestroy @compression

T-SQL uses the Chilkat ActiveX through the OLE Automation stored procedures. They must be enabled once on the server (EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;), and the Chilkat ActiveX registered must match the bitness of the SQL Server instance (64-bit for a 64-bit SQL Server). To bind to a specific major version of Chilkat, append the major version number to the ProgID, such as sp_OACreate 'Chilkat.Compression.11' for Chilkat v11.*.*.

sp_OACreate returns an object token (an int) that is passed as the first argument of every sp_OAMethod, sp_OAGetProperty and sp_OASetProperty call, and released with sp_OADestroy. Objects returned by methods (such as an HttpResponse or JsonObject) are also tokens received through an int OUT parameter; they must likewise be destroyed, and the OUT parameter is NULL when the method fails to return an object. Objects passed as arguments are passed by their token. When an OLE Automation procedure itself fails (non-zero @hr), sp_OAGetErrorInfo describes the error.

Data types: strings are nvarchar(4000); integers, booleans (1 or 0) and object tokens are int; dates are datetime. In the signatures on this page, @success, @iResult, @sResult and the like are the OUT variables receiving a method's return value, @iValue / @sValue receive or supply a property value, and the remaining @ variables are the method's arguments in order.

A string returned through an OUT parameter is limited to 4000 characters. For longer values, retrieve the result as a result set into a table variable instead of an OUT parameter, for example DECLARE @tmp TABLE (outputLine ntext) followed by INSERT INTO @tmp EXEC sp_OAGetProperty @compression, 'LastErrorText'. See string length limitations for strings returned by sp_OAMethod calls.

Methods that pass or return raw byte arrays are not shown on this page, because varbinary(max) values cannot be exchanged through sp_OAMethod (see varbinary(max) limitation). Use the BinData-based alternatives (methods ending in Bd) or the base64 / hex string-encoded variants instead. Binary properties (such as LastBinaryResult) can be retrieved as a result set into a table variable, as shown in their signatures. Asynchronous (*Async) methods and event callbacks are not available from SQL Server.

Properties

Algorithm
EXEC sp_OAGetProperty @compression, 'Algorithm', @sValue OUT
EXEC sp_OASetProperty @compression, 'Algorithm', @sValue

Specifies the compression algorithm to use. Supported values are deflate, zlib, bzip2, and lzw.

The zlib option is the deflate algorithm with a zlib header.

Note: ppmd is deprecated and should not be used. It was only available on 32-bit systems and specifically used the J variant. New applications should use one of the supported algorithms listed above.

top
Charset
EXEC sp_OAGetProperty @compression, 'Charset', @sValue OUT
EXEC sp_OASetProperty @compression, 'Charset', @sValue

Specifies the character encoding used when converting text to bytes before compression, and bytes back to text after decompression.

The current default is the computer’s ANSI charset, such as Windows-1252 on many Western Windows systems. However, most modern applications should explicitly set this property to utf-8.

Recommendation: Set Charset = "utf-8" unless you specifically need compatibility with another encoding.

top
DebugLogFilePath
EXEC sp_OAGetProperty @compression, 'DebugLogFilePath', @sValue OUT
EXEC sp_OASetProperty @compression, 'DebugLogFilePath', @sValue

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
DeflateLevel
EXEC sp_OAGetProperty @compression, 'DeflateLevel', @iValue OUT
EXEC sp_OASetProperty @compression, 'DeflateLevel', @iValue
Introduced in version 9.5.0.73

Sets the compression level used by the deflate and zlib algorithms.

  • 0 means no compression.
  • 9 means maximum compression.
  • The default value is 6.

Higher values may produce smaller output but can require more processing time.

top
EncodingMode
EXEC sp_OAGetProperty @compression, 'EncodingMode', @sValue OUT
EXEC sp_OASetProperty @compression, 'EncodingMode', @sValue

Specifies the text encoding used by methods whose names end in ENC, such as CompressBytesENC and DecompressStringENC.

Compression methods ending in ENC return compressed binary data as an encoded string. Decompression methods ending in ENC expect the input string to use this same encoding.

Valid values include:

  • base64
  • hex
  • url
  • quoted-printable

More Information and Examples
top
FirstChunk
EXEC sp_OAGetProperty @compression, 'FirstChunk', @iValue OUT
EXEC sp_OASetProperty @compression, 'FirstChunk', @iValue
Introduced in version 11.0.0

Indicates that the next chunk-aware compression or decompression call is the first chunk in a sequence.

The default value is 1.

When both FirstChunk and LastChunk are 1, the method call is treated as a complete, single-call compression or decompression operation.

top
LastBinaryResult
INSERT INTO @tmp EXEC sp_OAGetProperty @compression, 'LastBinaryResult'

This property is mainly used in SQL Server stored procedures to retrieve binary data from the last method call that returned binary data. It is only accessible if Chilkat.Global.KeepBinaryResult is set to 1. This feature allows for the retrieval of large varbinary results in an SQL Server environment, which has restrictions on returning large data via method calls, though temp tables can handle binary properties.

top
LastChunk
EXEC sp_OAGetProperty @compression, 'LastChunk', @iValue OUT
EXEC sp_OASetProperty @compression, 'LastChunk', @iValue
Introduced in version 11.0.0

Indicates that the next chunk-aware compression or decompression call is the final chunk in a sequence.

The default value is 1.

When both FirstChunk and LastChunk are 1, the input is treated as the complete data set and processed in a single call.

top
LastErrorHtml
EXEC sp_OAGetProperty @compression, 'LastErrorHtml', @sValue OUT

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
EXEC sp_OAGetProperty @compression, 'LastErrorText', @sValue OUT

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
EXEC sp_OAGetProperty @compression, 'LastErrorXml', @sValue OUT

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
EXEC sp_OAGetProperty @compression, 'LastMethodSuccess', @iValue OUT
EXEC sp_OASetProperty @compression, 'LastMethodSuccess', @iValue

Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
LastStringResult
EXEC sp_OAGetProperty @compression, 'LastStringResult', @sValue OUT

In SQL Server stored procedures, this property holds the string return value of the most recent method call that returns a string. It is accessible only when Chilkat.Global.KeepStringResult is set to TRUE. SQL Server has limitations on string lengths returned from methods and properties, but temp tables can be used to access large strings.

top
LastStringResultLen
EXEC sp_OAGetProperty @compression, 'LastStringResultLen', @iValue OUT

The length, in characters, of the string contained in the LastStringResult property.

top
UncommonOptions
EXEC sp_OAGetProperty @compression, 'UncommonOptions', @sValue OUT
EXEC sp_OASetProperty @compression, 'UncommonOptions', @sValue
Introduced in version 11.0.0

Provides a way to enable specialized or uncommon behavior. This property normally remains empty.

It may be set to a comma-separated list of keywords.

Supported option:

  • Crypt2CompressHdr — Duplicates the compression/decompression header behavior used by the deprecated and removed Crypt2 compression functions.

top
VerboseLogging
EXEC sp_OAGetProperty @compression, 'VerboseLogging', @iValue OUT
EXEC sp_OASetProperty @compression, 'VerboseLogging', @iValue

If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
EXEC sp_OAGetProperty @compression, 'Version', @sValue OUT

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

CompressBd
EXEC sp_OAMethod @compression, 'CompressBd', @success OUT, @bd
Introduced in version 9.5.0.66

Compresses the data contained in a BinData object.

The BinData object is modified to contain the compressed result.

This method is not FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
CompressBd2
EXEC sp_OAMethod @compression, 'CompressBd2', @success OUT, @bdIn, @bdOut
Introduced in version 11.0.0

Compresses the data in one BinData object and appends the compressed output to another BinData object.

The input BinData is not modified.

This method is FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
CompressEncryptFile
EXEC sp_OAMethod @compression, 'CompressEncryptFile', @success OUT, @cryptParams, @srcPath, @destPath
Introduced in version 9.5.0.99

Compresses and encrypts a file, writing the result to a destination file.

The compression and encryption are performed internally in streaming mode, so files of any size can be processed without loading the entire file into memory.

Returns 1 for success, 0 for failure.

top
CompressFile
EXEC sp_OAMethod @compression, 'CompressFile', @success OUT, @srcPath, @destPath

Compresses a source file and writes the compressed data to a destination file.

The file is processed internally in streaming mode, allowing files of any size to be compressed with stable memory usage.

Returns 1 for success, 0 for failure.

top
CompressSb
EXEC sp_OAMethod @compression, 'CompressSb', @success OUT, @sb, @binData
Introduced in version 9.5.0.73

Compresses the text contained in a StringBuilder and appends the compressed bytes to a BinData object.

Text is converted to bytes according to the Charset property.

This method is FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
CompressStr
EXEC sp_OAMethod @compression, 'CompressStr', @success OUT, @str, @bd
Introduced in version 11.0.0

Compresses a string and appends the compressed bytes to a BinData object.

The string is converted to bytes using the charset specified by the Charset property.

This method is FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
CompressStream
EXEC sp_OAMethod @compression, 'CompressStream', @success OUT, @strm
Introduced in version 9.5.0.56

Compresses data from a stream source and writes the compressed data to the stream sink.

The operation is performed in streaming mode, making it suitable for very large or even continuous streams while maintaining stable memory usage.

Returns 1 for success, 0 for failure.

top
CompressStringENC
EXEC sp_OAMethod @compression, 'CompressStringENC', @sResult OUT, @str

Compresses a string and returns the compressed result as an encoded string.

The string is first converted to bytes using Charset, then compressed, and finally encoded according to EncodingMode.

This method is not FirstChunk / LastChunk aware.

Returns NULL on failure

More Information and Examples
top
DecompressBd
EXEC sp_OAMethod @compression, 'DecompressBd', @success OUT, @bd
Introduced in version 9.5.0.66

Decompresses the compressed data contained in a BinData object.

The BinData object is modified to contain the decompressed result.

This method is not FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
DecompressBd2
EXEC sp_OAMethod @compression, 'DecompressBd2', @success OUT, @bdIn, @bdOut
Introduced in version 11.0.0

Decompresses the data in one BinData object and appends the decompressed output to another BinData object.

The input BinData is not modified.

This method is FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
DecompressFile
EXEC sp_OAMethod @compression, 'DecompressFile', @success OUT, @srcPath, @destPath

Decompresses a source file and writes the decompressed data to a destination file.

The file is processed internally in streaming mode, allowing files of any size to be decompressed without loading the entire file into memory.

Returns 1 for success, 0 for failure.

top
DecompressSb
EXEC sp_OAMethod @compression, 'DecompressSb', @success OUT, @binData, @sb
Introduced in version 9.5.0.73

Decompresses compressed data from a BinData object and appends the resulting text to a StringBuilder.

The decompressed bytes are converted to text using the Charset property.

This method is FirstChunk / LastChunk aware.

Returns 1 for success, 0 for failure.

top
DecompressStream
EXEC sp_OAMethod @compression, 'DecompressStream', @success OUT, @strm
Introduced in version 9.5.0.56

Decompresses data from a stream source and writes the decompressed data to the stream sink.

The operation is performed in streaming mode, making it suitable for very large or continuous streams while maintaining stable memory usage.

Returns 1 for success, 0 for failure.

top
DecompressStringENC
EXEC sp_OAMethod @compression, 'DecompressStringENC', @sResult OUT, @encodedCompressedData

Decompresses compressed data supplied as an encoded string and returns the resulting text.

The input string is decoded according to EncodingMode, then decompressed. The resulting bytes are converted to text using Charset.

This method is not FirstChunk / LastChunk aware.

Returns NULL on failure

More Information and Examples
top
DecryptDecompressFile
EXEC sp_OAMethod @compression, 'DecryptDecompressFile', @success OUT, @cryptParams, @srcPath, @destPath
Introduced in version 9.5.0.99

Decrypts and decompresses a file, writing the restored data to a destination file.

This is the reverse operation of CompressEncryptFile.

Returns 1 for success, 0 for failure.

top

Deprecated

BeginCompressStringENC
EXEC sp_OAMethod @compression, 'BeginCompressStringENC', @sResult OUT, @str
This method is deprecated and replaced by FirstChunk

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

Large amounts of string data may be compressed in chunks by first calling BeginCompressStringENC, followed by 0 or more calls to MoreCompressedStringENC, and ending with a final call to EndCompressStringENC. Each call returns 0 or more characters of compressed data (encoded as a string according to the EncodingMode property setting) which may be output to a compressed data stream (such as a file, socket, etc.).

Returns NULL on failure

top
BeginDecompressStringENC
EXEC sp_OAMethod @compression, 'BeginDecompressStringENC', @sResult OUT, @str
This method is deprecated and replaced by FirstChunk

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

The input to this method is an encoded string containing compressed data. The EncodingMode property should be set prior to calling this method. The input string is decoded according to the EncodingMode (hex, base64, etc.) and then decompressed.

A compressed data stream may be decompressed in chunks by first calling BeginDecompressStringENC, followed by 0 or more calls to MoreDecompressedStringENC, and ending with a final call to EndDecompressStringENC. Each call returns 0 or more characters of decompressed text.

Returns NULL on failure

top
EndCompressBytesENC
EXEC sp_OAMethod @compression, 'EndCompressBytesENC', @sResult OUT
This method is deprecated.

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

Must be callled to finalize a compression stream. Returns any remaining (buffered) compressed data.

(See BeginCompressBytesENC)

Returns NULL on failure

top
EndCompressStringENC
EXEC sp_OAMethod @compression, 'EndCompressStringENC', @sResult OUT
This method is deprecated.

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

Must be callled to finalize a compression stream. Returns any remaining (buffered) compressed data.

(See BeginCompressStringENC)

Returns NULL on failure

More Information and Examples
top
EndDecompressString
EXEC sp_OAMethod @compression, 'EndDecompressString', @sResult OUT
This method is deprecated.

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

Called to finalize the decompression stream and return any remaining (buffered) decompressed data.

(See BeginDecompressString)

Returns NULL on failure

More Information and Examples
top
EndDecompressStringENC
EXEC sp_OAMethod @compression, 'EndDecompressStringENC', @sResult OUT
This method is deprecated.

The Begin*, More*, and End* methods are replaced with the FirstChunk and LastChunk properties used in combination with FirstChunk/LastChunk aware methods, such as CompressStr and DecompressBd2.

Called to finalize the decompression stream and return any remaining (buffered) decompressed data.

The input to this method is an encoded string containing compressed data. The EncodingMode property should be set prior to calling this method. The input string is decoded according to the EncodingMode (hex, base64, etc.) and then decompressed.

(See BeginDecompressStringENC)

Returns NULL on failure

top
MoreCompressStringENC
EXEC sp_OAMethod @compression, 'MoreCompressStringENC', @sResult OUT, @str
This method is deprecated.

(See BeginCompressStringENC)

Returns NULL on failure

More Information and Examples
top
MoreDecompressStringENC
EXEC sp_OAMethod @compression, 'MoreDecompressStringENC', @sResult OUT, @str
This method is deprecated.

The input to this method is an encoded string containing compressed data. The EncodingMode property should be set prior to calling this method. The input string is decoded according to the EncodingMode (hex, base64, etc.) and then decompressed.

(See BeginDecompressStringENC)

Returns NULL on failure

top