Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

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 Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
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;
  pemStr: string;
  pattern: string;
  bHasMoreMatches: Boolean;

begin
  success := False;

  //  This example requires the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  zip := TZip.Create;

  //  Open a .zip containing PEM files, among other things..
  success := zip.OpenZip('qa_data/certs/thawte-roots.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  entry := TZipEntry.Create;

  pattern := '*.pem';

  bHasMoreMatches := zip.EntryMatching(pattern,entry);
  while bHasMoreMatches = True do
    begin

      WriteLn('Entry: ' + entry.FileName);

      //  Get the uncommpressed contents of the entry:
      pemStr := entry.UnzipToString(0,'utf-8');
      WriteLn(pemStr);

      bHasMoreMatches := entry.GetNextMatch(pattern);
    end;



  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.