SQL Server
SQL Server
Retrieve Metadata from Gzip Data in Memory (BinData)
This example demonstrates how to use the GetGzipInfoBd method to retrieve metadata from Gzip data stored in memory within a BinData object.
The Gzip data is first loaded into the BinData instance. The GetGzipInfoBd method then extracts any available metadata from the Gzip header, including the embedded filename, comment, and optional extra data.
The metadata 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.
Chilkat SQL Server Downloads
-- 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 from Gzip data
-- stored in a BinData object.
DECLARE @gzip int
EXEC @hr = sp_OACreate 'Chilkat.Gzip', @gzip OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
DECLARE @json int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT
-- Load a Gzip file into BinData:
EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'example.txt.gz'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @gzip
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @json
RETURN
END
-- Get the metadata information from the in-memory Gzip data:
EXEC sp_OAMethod @gzip, 'GetGzipInfoBd', @success OUT, @bd, @json
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @gzip
EXEC @hr = sp_OADestroy @bd
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 @bd
EXEC @hr = sp_OADestroy @json
END
GO