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

Unzip Some Files by Iterating over Entries

See more Zip Examples

Demonstrates how to unzip specific files by iterating over entries in a .zip.

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.StringBuilder,
  Chilkat.ZipEntry,
  Chilkat.Zip;

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

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  sbFilename: TStringBuilder;
  entry: TZipEntry;
  numEntries: Integer;
  i: Integer;
  entryFilePath: string;

begin
  success := False;

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

  zip := TZip.Create;

  //  Open a .zip containing:
  //  
  //  a1.xml
  //  b1.xml
  //  c1.xml
  //  dir1/a2.xml
  //  dir1/c2.xml
  //  dir2/dir3/c3.xml

  //  We wish to unzip only a1.xml, b1.xml, and c1.xml
  success := zip.OpenZip('qa_data/zips/xml_files.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  sbFilename := TStringBuilder.Create;

  entry := TZipEntry.Create;
  numEntries := zip.NumEntries;
  i := 0;
  while i < numEntries do
    begin
      zip.EntryAt(i,entry);

      entryFilePath := entry.FileName;
      WriteLn(entryFilePath);

      if (entry.IsDirectory = False) then
        begin
          sbFilename.SetString(entryFilePath);
          if (sbFilename.Contains('/',False) = False) then
            begin
              //  Does not contain "/"
              //  Unzip to the qa_output directory.
              success := entry.Extract('qa_output');
              if (success = False) then
                begin
                  WriteLn('Failed to unzip ' + entryFilePath);
                end
              else
                begin
                  WriteLn('Unzipped ' + entryFilePath);
                end;
            end;
        end;

      i := i + 1;
    end;



  zip.Free;
  sbFilename.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.