SQL Server
SQL Server
Get Compressed ZIP Entry Data as Base64 Using ZipEntry.CopyToBase64
See more Zip Examples
This example creates a small ZIP archive containing a text file added from memory, writes the ZIP to disk, re-opens it, and then uses ZipEntry.CopyToBase64 to retrieve the compressed data for the text file as a Base64-encoded string.
The Base64 string returned by CopyToBase64 represents the compressed bytes stored for the entry inside the ZIP archive. It is not the original uncompressed text data.
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
-- ------------------------------------------------------------
-- First create a sample ZIP archive containing a small text file.
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, 'c:/temp/sampleText.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- Add a small text file from memory.
-- The text is stored in the ZIP as "hello.txt".
EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'hello.txt', 'Hello from a ZIP entry!', 'utf-8'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
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
RETURN
END
-- ------------------------------------------------------------
-- Now open the ZIP archive we just created.
DECLARE @zip2 int
EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip2 OUT
EXEC sp_OAMethod @zip2, 'OpenZip', @success OUT, 'c:/temp/sampleText.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @zip2
RETURN
END
-- Get the text file entry.
DECLARE @entry int
EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT
EXEC sp_OAMethod @zip2, 'EntryOf', @success OUT, 'hello.txt', @entry
IF @success = 0
BEGIN
PRINT 'Entry not found.'
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @zip2
EXEC @hr = sp_OADestroy @entry
RETURN
END
-- Get the compressed ZIP entry data as Base64.
--
-- This is the compressed data stored inside the ZIP file,
-- not the uncompressed text "Hello from a ZIP entry!".
DECLARE @b64 nvarchar(4000)
EXEC sp_OAMethod @entry, 'CopyToBase64', @b64 OUT
PRINT 'Base64 compressed ZIP entry data:'
PRINT @b64
EXEC sp_OAMethod @zip2, 'CloseZip', NULL
PRINT 'Done.'
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @zip2
EXEC @hr = sp_OADestroy @entry
END
GO