Sample code for 30+ languages & platforms
SQL Server

Iterate over Matching Filenames

See more Zip Examples

Iterates over files within a .zip that match a pattern. In this case, the pattern is "*.pem". This example will open a .zip containing files of type .txt, .cer, .pem, and possibly others, and will iterate over only the .pem files.

Chilkat SQL Server Downloads

SQL Server
-- 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 requires 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

    -- Open a .zip containing PEM files, among other things..
    EXEC sp_OAMethod @zip, 'OpenZip', @success OUT, 'qa_data/certs/thawte-roots.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

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

    DECLARE @pemStr nvarchar(4000)

    DECLARE @pattern nvarchar(4000)
    SELECT @pattern = '*.pem'

    DECLARE @bHasMoreMatches int
    EXEC sp_OAMethod @zip, 'EntryMatching', @bHasMoreMatches OUT, @pattern, @entry
    WHILE @bHasMoreMatches = 1
      BEGIN


        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT
        PRINT 'Entry: ' + @sTmp0

        -- Get the uncommpressed contents of the entry:
        EXEC sp_OAMethod @entry, 'UnzipToString', @pemStr OUT, 0, 'utf-8'

        PRINT @pemStr

        EXEC sp_OAMethod @entry, 'GetNextMatch', @bHasMoreMatches OUT, @pattern
      END

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


END
GO