Sample code for 30+ languages & platforms
Lazarus Pascal

Iterate Through ZIP Entries Using EntryAt and ZipEntry.GetNext

See more Zip Examples

This example demonstrates how to iterate through all entries in a ZIP archive using:

  • Zip.EntryAt to obtain the first entry
  • ZipEntry.GetNext to advance through the remaining entries

The example prints each ZIP entry's stored filename and whether the entry is a file or directory.

This approach is useful when sequentially processing ZIP entries without repeatedly calling EntryAt by index.

Suppose the ZIP archive contains:

docs/
docs/readme.txt
images/logo.png
hello.txt

The example iterates through each entry in the order stored within the ZIP archive.

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/example.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  A ZIP archive may contain zero entries.
  if (zip.NumEntries = 0) then
    begin

      WriteLn('The ZIP archive is empty.');

      zip.CloseZip();
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Get the first ZIP entry.
  //  
  //  EntryAt(0,entry) initializes the ZipEntry object so that
  //  it represents the first entry in the ZIP archive.
  //  
  entry := TZipEntry.Create;

  success := zip.EntryAt(0,entry);
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  //  ------------------------------------------------------------
  //  Iterate through all ZIP entries.
  //  
  //  GetNext updates the same ZipEntry object so that it
  //  represents the next entry in the ZIP archive.
  //  
  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 entry.
      success := entry.GetNext();
    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.