Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkZip.pb"
IncludeFile "CkZipEntry.pb"

Procedure ChilkatExample()

    success.i = 0

    zip.i = CkZip::ckCreate()
    If zip.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Open an existing ZIP archive.
    success = CkZip::ckOpenZip(zip,"c:/temp/sample.zip")
    If success = 0
        Debug CkZip::ckLastErrorText(zip)
        CkZip::ckDispose(zip)
        ProcedureReturn
    EndIf

    ; ------------------------------------------------------------
    ; Find the first ZIP entry matching the wildcard pattern:
    ; 
    ;     docs/*
    ; 
    ; Matching is performed against the full stored ZIP path.
    ; 
    entry.i = CkZipEntry::ckCreate()
    If entry.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkZip::ckEntryMatching(zip,"docs/*",entry)
    If success = 0
        Debug "No matching entries found."
        CkZip::ckCloseZip(zip)
        CkZip::ckDispose(zip)
        CkZipEntry::ckDispose(entry)
        ProcedureReturn
    EndIf

    ; ------------------------------------------------------------
    ; Iterate through all matching entries.
    ; 
    ; GetNextMatch updates the same ZipEntry object so that
    ; it represents the next matching entry.
    ; 
    While (success = 1)
        If CkZipEntry::ckIsDirectory(entry) = 1
            Debug "[Directory] " + CkZipEntry::ckFileName(entry)
        Else
            Debug "[File] " + CkZipEntry::ckFileName(entry)
        EndIf

        ; Advance to the next matching entry.
        success = CkZipEntry::ckGetNextMatch(entry,"docs/*")
    Wend

    CkZip::ckCloseZip(zip)

    Debug "Done."


    CkZip::ckDispose(zip)
    CkZipEntry::ckDispose(entry)


    ProcedureReturn
EndProcedure