![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Compress Text from StringBuilder to Gzip (BinData Output)See more Gzip ExamplesThis example demonstrates how to use the The text is first converted to its byte representation using the specified character set (in this case, UTF-8). These bytes are then compressed, and the resulting Gzip data is written to a This approach is useful when working with dynamically generated text that you want to compress without first writing it to a file. The example also shows how the compressed data can optionally be saved to a
-- 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 compress text contained in a StringBuilder -- into Gzip format, storing the compressed result 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 @sb int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT DECLARE @bd int EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. -- Add some text to the StringBuilder: EXEC sp_OAMethod @sb, 'Append', @success OUT, 'The quick brown fox jumps over the lazy dog.' -- Compress the text using UTF-8 encoding: EXEC sp_OAMethod @gzip, 'CompressSb', @success OUT, @sb, 'utf-8', @bd IF @success = 0 BEGIN EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @gzip EXEC @hr = sp_OADestroy @sb EXEC @hr = sp_OADestroy @bd RETURN END -- The BinData now contains the Gzip-compressed bytes. PRINT 'Compression successful.' EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT PRINT 'Compressed size (bytes): ' + @iTmp0 -- (Optional) Save to a .gz file: EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, 'text.gz' IF @success = 0 BEGIN EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @gzip EXEC @hr = sp_OADestroy @sb EXEC @hr = sp_OADestroy @bd RETURN END PRINT 'Gzip file written to text.gz' EXEC @hr = sp_OADestroy @gzip EXEC @hr = sp_OADestroy @sb EXEC @hr = sp_OADestroy @bd END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.