Sample code for 30+ languages & platforms
Lazarus Pascal

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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.ZipEntry,
  Chilkat.Zip;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  entry: TZipEntry;

begin
  success := False;

  zip := TZip.Create;

  //  Open an existing ZIP archive.
  success := zip.OpenZip('c:/temp/sample.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Find the first ZIP entry matching the wildcard pattern:
  //  
  //      docs/*
  //  
  //  Matching is performed against the full stored ZIP path.
  //  
  entry := TZipEntry.Create;

  success := zip.EntryMatching('docs/*',entry);
  if (success = False) then
    begin
      WriteLn('No matching entries found.');
      zip.CloseZip();
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Iterate through all matching entries.
  //  
  //  GetNextMatch updates the same ZipEntry object so that
  //  it represents the next matching entry.
  //  
  while (success = True) do
    begin
      if (entry.IsDirectory = True) then
        begin
          WriteLn('[Directory] ' + entry.FileName);
        end
      else
        begin
          WriteLn('[File] ' + entry.FileName);
        end;

      //  Advance to the next matching entry.
      success := entry.GetNextMatch('docs/*');
    end;

  zip.CloseZip();

  WriteLn('Done.');


  zip.Free;
  entry.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.