Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- 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

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.