Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Unzip Files to Byte Array

See more Zip Examples

Demonstrates how to unzip each file contained in a .zip to an in-memory byte array.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    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

    EXEC sp_OAMethod @zip, 'OpenZip', @success OUT, 'qa_data/zips/test.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    --  Iterate of each entry in the zip.
    --  An entry can be a file or directory entry.  For each file, unzip to a byte array.
    DECLARE @numEntries int
    EXEC sp_OAGetProperty @zip, 'NumEntries', @numEntries OUT

    PRINT 'NumEntries = ' + @numEntries

    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numEntries
      BEGIN
        EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry
        EXEC sp_OAGetProperty @entry, 'IsDirectory', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
ERROR: Cannot use pbytes in SQL Server examples...
            EXEC sp_OAMethod @entry, 'Inflate', @fileData OUT
            --  Do whatever you wish with the file data...
          END
        SELECT @i = @i + 1
      END

    EXEC sp_OAMethod @zip, 'CloseZip', NULL


    PRINT 'Finished.'

    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @entry


END
GO