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

Unzip Selected Files from a Zip Archive

See more Zip Examples

Demonstrates how to iterate over the files contained within a .zip and unzip specific 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;
  unzipDir: string;
  n: Integer;
  entry: TZipEntry;
  i: Integer;

begin
  success := False;

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

  zip := TZip.Create;

  success := zip.OpenZip('my_files.zip');
  if (success = False) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  unzipDir := '/temp/unzipDir';

  //  Get the number of files and directories in the .zip
  n := zip.NumEntries;

  entry := TZipEntry.Create;

  i := 0;
  while i < n do
    begin

      zip.EntryAt(i,entry);
      if (entry.IsDirectory = False) then
        begin
          //  (the filename may include a path)
          WriteLn(entry.FileName);

          //  Your application may choose to unzip this entry
          //  based on the filename.
          //  If the entry should be unzipped, then call Extract(unzipDir)
          success := entry.Extract(unzipDir);
          if (success = False) then
            begin
              WriteLn(entry.LastErrorText);
              Exit;
            end;

        end;
      i := i + 1;
    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.