Sample code for 30+ languages & platforms
SQL Server

Iterate Through Matching ZIP Entries Using EntryMatching and ZipEntry.GetNextMatch

See more Zip Examples

This example demonstrates how to iterate through ZIP entries matching a wildcard pattern using:

  • Zip.EntryMatching to obtain the first matching entry
  • ZipEntry.GetNextMatch to advance to subsequent matching entries

The wildcard character * matches zero or more characters. Matching is performed against the full stored ZIP entry path.

The example searches for all entries beneath the docs/ directory.

Suppose the ZIP archive contains:

docs/
docs/readme.txt
docs/manual.pdf
docs/sub1/notes.txt
images/logo.png
hello.txt

The wildcard pattern:

docs/*

Matches:

docs/
docs/readme.txt
docs/manual.pdf
docs/sub1/notes.txt

Note that ZIP archives may optionally contain separate directory entries. Therefore, the first matching entry may be the directory entry docs/ rather than a file entry.

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
    DECLARE @iTmp0 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, 'c:/temp/sample.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- ------------------------------------------------------------
    -- Find the first ZIP entry matching the wildcard pattern:
    -- 
    --     docs/*
    -- 
    -- Matching is performed against the full stored ZIP path.
    -- 
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    EXEC sp_OAMethod @zip, 'EntryMatching', @success OUT, 'docs/*', @entry
    IF @success = 0
      BEGIN

        PRINT 'No matching entries found.'
        EXEC sp_OAMethod @zip, 'CloseZip', NULL
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    -- ------------------------------------------------------------
    -- Iterate through all matching entries.
    -- 
    -- GetNextMatch updates the same ZipEntry object so that
    -- it represents the next matching entry.
    -- 
    WHILE (@success = 1)
      BEGIN
        EXEC sp_OAGetProperty @entry, 'IsDirectory', @iTmp0 OUT
        IF @iTmp0 = 1
          BEGIN

            EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT
            PRINT '[Directory] ' + @sTmp0
          END
        ELSE
          BEGIN

            EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT
            PRINT '[File] ' + @sTmp0
          END

        -- Advance to the next matching entry.
        EXEC sp_OAMethod @entry, 'GetNextMatch', @success OUT, 'docs/*'
      END

    EXEC sp_OAMethod @zip, 'CloseZip', NULL


    PRINT 'Done.'

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


END
GO