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

List Files/Directories in Zip by Index

See more Zip Examples

Demonstrates how to iterate over the files and directories in a zip archive by index.

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;
  numEntries: Integer;
  entry: TZipEntry;
  i: Integer;

begin
  success := False;

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

  zip := TZip.Create;

  success := zip.OpenZip('qa_data/zips/sample.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  numEntries := zip.NumEntries;

  entry := TZipEntry.Create;

  i := 0;
  while i < numEntries do
    begin
      zip.EntryAt(i,entry);
      //  Note: In the ZIP file format, it is not required for a ZIP file to contain explicit directory entries. 
      //  However, explicit directory entries can be included for convenience and compatibility with certain ZIP file processing tools and software.
      //  An explicit directory entry in a ZIP file is an entry that specifically represents a directory, rather than a file. 
      //  It usually has a directory name with a trailing slash (/) and zero-length content.
      if (entry.IsDirectory) then
        begin
          WriteLn(i + ': ' + entry.FileName + ' (directory)');
        end
      else
        begin
          WriteLn(i + ': ' + entry.FileName);
        end;
      i := i + 1;
    end;

  //  Sample output:

  //  0: aaa/ (directory)
  //  1: aaa/pigs.json
  //  2: bbb/ (directory)
  //  3: bbb/base64Cert.txt
  //  4: bbb/sub1/ (directory)
  //  5: bbb/sub1/brasil_cert.pem
  //  6: bbb/sub2/ (directory)
  //  7: bbb/sub2/penguins.gif
  //  8: bbb/sub2/starfish.jpg
  //  9: hamlet.xml
  //  10: hello.pdf

  zip.CloseZip();


  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.