Sample code for 30+ languages & platforms
SQL Server

Add Custom Extra Data to a Gzip File

This example demonstrates how to use the SetExtraData method to include custom binary data in the Gzip header using a hex-encoded string.

The hex string represents the raw bytes to embed in the Gzip metadata. When a compression method is called, this data is included in the Gzip header.

The example also shows how to retrieve the metadata using GetGzipInfo to verify that the extra data was successfully embedded. The retrieved value is returned as a Base64-encoded string.

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 demonstrates how to include custom extra data
    -- in the Gzip header when compressing.

    -- The extra data is provided as a hex-encoded string.

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    -- Set extra data using a hex string.
    -- This example represents 4 bytes: 00 01 02 03
    EXEC sp_OAMethod @gzip, 'SetExtraData', @success OUT, '00010203', 'hex'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @gzip
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    -- Compress a file so the extra data is embedded in the Gzip header:
    DECLARE @inputFile nvarchar(4000)
    SELECT @inputFile = 'example.txt'
    DECLARE @outputFile nvarchar(4000)
    SELECT @outputFile = 'example.txt.gz'

    EXEC sp_OAMethod @gzip, 'CompressFile', @success OUT, @inputFile, @outputFile
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @gzip
        EXEC @hr = sp_OADestroy @json
        RETURN
      END


    PRINT 'Gzip file created with extra data.'

    -- (Optional) Retrieve the metadata to verify:
    EXEC sp_OAMethod @gzip, 'GetGzipInfo', @success OUT, @outputFile, @json
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @gzip
        EXEC @hr = sp_OADestroy @json
        RETURN
      END


    PRINT 'Metadata JSON:'
    EXEC sp_OAMethod @json, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @gzip
    EXEC @hr = sp_OADestroy @json


END
GO