![]() |
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) Chunked Compression Using CompressBd2 with FirstChunk and LastChunkSee more Compression ExamplesThis example demonstrates how to compress data in multiple segments using theCompressBd2 method together with the FirstChunk and LastChunk properties. Instead of compressing all data in a single call, the input is divided into smaller chunks and processed sequentially, which is useful when handling streaming data or large inputs.
The example shows how to correctly mark the first, middle, and final chunks so that the compression stream is properly initialized and finalized. The compressed output is accumulated in a separate This approach is ideal for scenarios where data is received incrementally, such as reading from a network stream, processing large files in parts, or handling real-time data feeds.
-- 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 has already been unlocked. -- See Global Unlock Sample for sample code. DECLARE @compress int EXEC @hr = sp_OACreate 'Chilkat.Compression', @compress OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END EXEC sp_OASetProperty @compress, 'Algorithm', 'zlib' -- This will accumulate the compressed output. DECLARE @bdOut int EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdOut OUT -- ------------------------------------------------------------------ -- Simulate input arriving in chunks. -- ------------------------------------------------------------------ DECLARE @part1 nvarchar(4000) SELECT @part1 = 'The quick brown fox ' DECLARE @part2 nvarchar(4000) SELECT @part2 = 'jumps over the lazy dog. ' DECLARE @part3 nvarchar(4000) SELECT @part3 = 'This text is split into chunks.' -- ------------------------------------------------------------------ -- Compress the first chunk -- ------------------------------------------------------------------ EXEC sp_OASetProperty @compress, 'FirstChunk', 1 EXEC sp_OASetProperty @compress, 'LastChunk', 0 DECLARE @bdIn int EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdIn OUT EXEC sp_OAMethod @bdIn, 'AppendString', @success OUT, @part1, 'utf-8' EXEC sp_OAMethod @compress, 'CompressBd2', @success OUT, @bdIn, @bdOut IF @success = 0 BEGIN EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @compress EXEC @hr = sp_OADestroy @bdOut EXEC @hr = sp_OADestroy @bdIn RETURN END -- ------------------------------------------------------------------ -- Compress a middle chunk -- ------------------------------------------------------------------ EXEC sp_OASetProperty @compress, 'FirstChunk', 0 EXEC sp_OASetProperty @compress, 'LastChunk', 0 EXEC sp_OAMethod @bdIn, 'Clear', @success OUT EXEC sp_OAMethod @bdIn, 'AppendString', @success OUT, @part2, 'utf-8' EXEC sp_OAMethod @compress, 'CompressBd2', @success OUT, @bdIn, @bdOut IF @success = 0 BEGIN EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @compress EXEC @hr = sp_OADestroy @bdOut EXEC @hr = sp_OADestroy @bdIn RETURN END -- ------------------------------------------------------------------ -- Compress the final chunk -- ------------------------------------------------------------------ EXEC sp_OASetProperty @compress, 'FirstChunk', 0 EXEC sp_OASetProperty @compress, 'LastChunk', 1 EXEC sp_OAMethod @bdIn, 'Clear', @success OUT EXEC sp_OAMethod @bdIn, 'AppendString', @success OUT, @part3, 'utf-8' EXEC sp_OAMethod @compress, 'CompressBd2', @success OUT, @bdIn, @bdOut IF @success = 0 BEGIN EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @compress EXEC @hr = sp_OADestroy @bdOut EXEC @hr = sp_OADestroy @bdIn RETURN END -- Get the final compressed result as base64 for display DECLARE @compressedBase64 nvarchar(4000) EXEC sp_OAMethod @bdOut, 'GetEncoded', @compressedBase64 OUT, 'base64' PRINT 'Compressed data (base64):' PRINT @compressedBase64 -- ------------------------------------------------------------------ -- Decompress to verify correctness -- ------------------------------------------------------------------ DECLARE @bdDecompressed int EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdDecompressed OUT EXEC sp_OASetProperty @compress, 'FirstChunk', 1 EXEC sp_OASetProperty @compress, 'LastChunk', 1 EXEC sp_OAMethod @compress, 'DecompressBd2', @success OUT, @bdOut, @bdDecompressed IF @success = 0 BEGIN EXEC sp_OAGetProperty @compress, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @compress EXEC @hr = sp_OADestroy @bdOut EXEC @hr = sp_OADestroy @bdIn EXEC @hr = sp_OADestroy @bdDecompressed RETURN END -- Convert decompressed bytes back to a string DECLARE @resultText nvarchar(4000) EXEC sp_OAMethod @bdDecompressed, 'GetString', @resultText OUT, 'utf-8' PRINT 'Decompressed text:' PRINT @resultText EXEC @hr = sp_OADestroy @compress EXEC @hr = sp_OADestroy @bdOut EXEC @hr = sp_OADestroy @bdIn EXEC @hr = sp_OADestroy @bdDecompressed END GO |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.