Sample code for 30+ languages & platforms
SQL Server

Compressing and Decompressing Files Using Streaming (CompressFile / DecompressFile)

See more Compression Examples

This example demonstrates how to compress a file to a binary format and then restore it using the Chilkat.Compression class. The CompressFile method reads the source file, compresses it using the specified algorithm, and writes the result to a destination file. The DecompressFile method performs the reverse operation, restoring the original file from the compressed data.

Both operations are performed internally in streaming mode, allowing files of any size to be processed efficiently without loading the entire file into memory. The example also includes a simple verification step by comparing file sizes to confirm that the decompressed output matches the original input.

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.

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

    -- Use the zlib algorithm (recommended for general use)
    EXEC sp_OASetProperty @compress, 'Algorithm', 'zlib'

    -- ------------------------------------------------------------------
    -- Compress a file
    -- ------------------------------------------------------------------

    DECLARE @inputFile nvarchar(4000)
    SELECT @inputFile = 'c:/temp/example.txt'
    DECLARE @compressedFile nvarchar(4000)
    SELECT @compressedFile = 'c:/temp/example.txt.zlib'

    EXEC sp_OAMethod @compress, 'CompressFile', @success OUT, @inputFile, @compressedFile
    IF @success = 0
      BEGIN

        PRINT 'Compression failed:'
        EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @compress
        RETURN
      END


    PRINT 'File compressed successfully:'

    PRINT '  Input:      ' + @inputFile

    PRINT '  Compressed: ' + @compressedFile

    -- ------------------------------------------------------------------
    -- Decompress the file back to its original form
    -- ------------------------------------------------------------------

    DECLARE @decompressedFile nvarchar(4000)
    SELECT @decompressedFile = 'c:/temp/example_restored.txt'

    EXEC sp_OAMethod @compress, 'DecompressFile', @success OUT, @compressedFile, @decompressedFile
    IF @success = 0
      BEGIN

        PRINT 'Decompression failed:'
        EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @compress
        RETURN
      END


    PRINT 'File decompressed successfully:'

    PRINT '  Output: ' + @decompressedFile

    -- ------------------------------------------------------------------
    -- Optional: Verify file sizes (basic sanity check)
    -- ------------------------------------------------------------------

    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    DECLARE @originalSize int
    EXEC sp_OAMethod @fac, 'FileSize', @originalSize OUT, @inputFile
    DECLARE @restoredSize int
    EXEC sp_OAMethod @fac, 'FileSize', @restoredSize OUT, @decompressedFile


    PRINT 'Original file size:   ' + @originalSize

    PRINT 'Restored file size:   ' + @restoredSize

    IF @originalSize = @restoredSize
      BEGIN

        PRINT 'Sizes match (basic verification successful).'
      END
    ELSE
      BEGIN

        PRINT 'Warning: File sizes differ.'
      END

    EXEC @hr = sp_OADestroy @compress
    EXEC @hr = sp_OADestroy @fac


END
GO