Sample code for 30+ languages & platforms
SQL Server

Retrieve Metadata from a Gzip File

See more Gzip Examples

This example demonstrates how to use the GetGzipInfo method to retrieve metadata embedded within a Gzip file.

The method reads the Gzip header and extracts any available metadata, including the embedded filename, comment, and optional extra data. The extracted information is returned in a JsonObject.

Because these fields are optional, the example checks for the existence of each JSON member using HasMember before retrieving its value. This avoids attempting to access values that may not be present.

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
    DECLARE @iTmp0 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 retrieve metadata embedded in a Gzip file.

    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

    -- The Gzip file to examine:
    DECLARE @gzPath nvarchar(4000)
    SELECT @gzPath = 'example.txt.gz'

    -- Get the metadata information:
    EXEC sp_OAMethod @gzip, 'GetGzipInfo', @success OUT, @gzPath, @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

    -- Output the JSON containing metadata:

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

    -- Access individual fields only if they exist:

    EXEC sp_OAMethod @json, 'HasMember', @iTmp0 OUT, 'filename'
    IF @iTmp0 = 1
      BEGIN
        DECLARE @filename nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @filename OUT, 'filename'

        PRINT 'Filename: ' + @filename
      END

    EXEC sp_OAMethod @json, 'HasMember', @iTmp0 OUT, 'comment'
    IF @iTmp0 = 1
      BEGIN
        DECLARE @comment nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @comment OUT, 'comment'

        PRINT 'Comment: ' + @comment
      END

    EXEC sp_OAMethod @json, 'HasMember', @iTmp0 OUT, 'extraData'
    IF @iTmp0 = 1
      BEGIN
        DECLARE @extraData nvarchar(4000)
        EXEC sp_OAMethod @json, 'StringOf', @extraData OUT, 'extraData'

        PRINT 'ExtraData (Base64): ' + @extraData
      END

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


END
GO