SQL Server
SQL Server
Create Zip in a BinData Object
See more Zip Examples
Recursively appends files in a directory tree and writes a zip archive into a Chilkat BinData object.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 @zip int
EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Initialize the zip object. Because we will never actually write a zip file to the filesystem,
-- the filepath passed to NewZip does not matter.
EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'x.zip'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- Append a directory tree. The call to AppendFiles does
-- not read the file contents or append them to the zip
-- object in memory. It simply appends references
-- to the files so that when WriteBd, WriteZip, or WriteZipAndClose
-- is called, the referenced files are streamed and compressed
-- into the .zip output file (or BinData object).
DECLARE @recurse int
SELECT @recurse = 1
EXEC sp_OAMethod @zip, 'AppendFiles', @success OUT, 'c:/temp/a/*', @recurse
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- Write the zip archive into the bdZip object.
DECLARE @bdZip int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdZip OUT
EXEC sp_OAMethod @zip, 'WriteBd', @success OUT, @bdZip
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bdZip
RETURN
END
-- We could directly access the bytes of the zip archive, or perhaps
-- get the zip bytes in base64 format.
DECLARE @zipAsBase64 nvarchar(4000)
EXEC sp_OAMethod @bdZip, 'GetEncoded', @zipAsBase64 OUT, 'base64'
PRINT @zipAsBase64
-- Or the zip can be used by some other Chilkat method call that accepts
-- a BinData object as an argument.
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @bdZip
END
GO