SQL Server
SQL Server
Unzip a Text File Entry into a StringBuilder Using ZipEntry.UnzipToSb
See more Zip Examples
This example demonstrates how to use the ZipEntry.UnzipToSb method to inflate a text file entry directly into a StringBuilder object.
The ZIP entry is uncompressed entirely in memory and appended to the StringBuilder without creating a temporary file on disk.
This is useful when:
- Processing text files directly in memory
- Avoiding temporary filesystem files
- Reading configuration files, JSON, XML, CSV, or source code from ZIP archives
- Automatically converting line endings while inflating the text
The srcCharset argument specifies how the uncompressed bytes should be interpreted, such as:
-
utf-8 -
utf-16 -
windows-1252
The lineEndingBehavior argument controls line-ending conversion:
-
0— Leave line endings unchanged -
1— Convert all line endings to bare LF -
2— Convert all line endings to CRLF
Suppose the ZIP archive contains:
config/settings.json The example inflates the JSON file directly into a StringBuilder.
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
DECLARE @zip int
EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Open an existing ZIP archive.
EXEC sp_OAMethod @zip, 'OpenZip', @success OUT, 'qa_data/zips/configFiles.zip'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
RETURN
END
-- Locate the JSON file entry within the ZIP archive.
DECLARE @entry int
EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT
EXEC sp_OAMethod @zip, 'EntryOf', @success OUT, 'config/settings.json', @entry
IF @success = 0
BEGIN
PRINT 'ZIP entry not found.'
EXEC sp_OAMethod @zip, 'CloseZip', NULL
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @entry
RETURN
END
-- ------------------------------------------------------------
-- Inflate the ZIP entry directly into a StringBuilder.
--
-- The uncompressed text is appended to the StringBuilder.
--
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
-- Leave line endings unchanged.
DECLARE @lineEndingBehavior int
SELECT @lineEndingBehavior = 0
-- Interpret the uncompressed bytes as UTF-8 text.
DECLARE @srcCharset nvarchar(4000)
SELECT @srcCharset = 'utf-8'
EXEC sp_OAMethod @entry, 'UnzipToSb', @success OUT, @lineEndingBehavior, @srcCharset, @sb
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @entry, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @sb
RETURN
END
PRINT 'Uncompressed text:'
PRINT ''
EXEC sp_OAMethod @sb, 'GetAsString', @sTmp0 OUT
PRINT @sTmp0
EXEC sp_OAMethod @zip, 'CloseZip', NULL
PRINT 'Done.'
EXEC @hr = sp_OADestroy @zip
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @sb
END
GO