SQL Server
SQL Server
Add Binary Data to a ZIP Using AddBd
See more Zip Examples
This example demonstrates how to use the AddBd method to add binary data from a BinData object as a file entry within a ZIP archive.
The data is created entirely in memory, added to the ZIP as data/binary.dat, and then written to disk.
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
-- Create a new ZIP archive.
DECLARE @zip int
EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'inMemoryData.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- Create a BinData object containing binary content.
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
-- Append some bytes as hexadecimal.
-- The decoded bytes will become the contents of the ZIP entry.
EXEC sp_OAMethod @bd, 'AppendEncoded', @success OUT, '000102030405060708090A0B0C0D0E0F', 'hex'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @bd, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bd
RETURN
END
-- Add the BinData contents as a file entry within the ZIP.
-- The file will be stored as "data/binary.dat" inside the ZIP archive.
EXEC sp_OAMethod @zip, 'AddBd', @success OUT, 'data/binary.dat', @bd
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bd
RETURN
END
-- Write the ZIP archive to disk and close it.
EXEC sp_OAMethod @zip, 'WriteZipAndClose', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bd
RETURN
END
PRINT 'ZIP archive created successfully.'
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bd
END
GO