SQL Server
SQL Server
Gzip Compress In Memory and Base64 Encode
See more Gzip Examples
Demonstrates how to Gzip compress in-memory data and then encode the compressed data to base64.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
-- 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 to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @gzip int
EXEC @hr = sp_OACreate 'Chilkat.Gzip', @gzip OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- This example will load a file into the fileData object.
-- Your application might load fileData from other sources..
DECLARE @fileData int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @fileData OUT
EXEC sp_OAMethod @fileData, 'LoadFile', @success OUT, 'qa_data/xml/hamlet.xml'
IF @success <> 1
BEGIN
PRINT 'Failed to load file.'
EXEC @hr = sp_OADestroy @gzip
EXEC @hr = sp_OADestroy @fileData
RETURN
END
-- In-place compress the contents of fileData
EXEC sp_OAMethod @gzip, 'CompressBd', @success OUT, @fileData
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @gzip
EXEC @hr = sp_OADestroy @fileData
RETURN
END
-- Get the base64 encoded compressed data (in a single line).
DECLARE @strBase64 nvarchar(4000)
EXEC sp_OAMethod @fileData, 'GetEncoded', @strBase64 OUT, 'base64'
PRINT @strBase64
PRINT '--------'
-- To get the base64 in multiple lines, as it might appear in MIME,
-- use "base64-mime".
DECLARE @strBase64MultiLine nvarchar(4000)
EXEC sp_OAMethod @fileData, 'GetEncoded', @strBase64MultiLine OUT, 'base64-mime'
PRINT @strBase64MultiLine
EXEC @hr = sp_OADestroy @gzip
EXEC @hr = sp_OADestroy @fileData
END
GO