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

Unzip an Entire ZIP Archive While Preserving Directory Structure

See more Zip Examples

Extract an Entire ZIP Archive While Preserving Directory Structure

This example demonstrates how to use the Unzip method to extract all files and directories from a ZIP archive.

The example shows how the stored ZIP directory structure is recreated beneath the target extraction directory.

Suppose the ZIP archive contains:

aaa/pigs.json
bbb/base64Cert.txt
bbb/sub1/brasil_cert.pem
bbb/sub2/penguins.gif
bbb/sub2/starfish.jpg
hamlet.xml
hello.pdf

After extraction to:

c:/temp/testDir

The following filesystem structure will be created:

c:/temp/testDir/aaa/pigs.json
c:/temp/testDir/bbb/base64Cert.txt
c:/temp/testDir/bbb/sub1/brasil_cert.pem
c:/temp/testDir/bbb/sub2/penguins.gif
c:/temp/testDir/bbb/sub2/starfish.jpg
c:/temp/testDir/hamlet.xml
c:/temp/testDir/hello.pdf

If the target extraction directory does not already exist, it is automatically created.

The Unzip method returns the number of files extracted, or -1 if the extraction fails.

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.Zip;

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

procedure RunDemo;
var
  success: Boolean;
  zip: TZip;
  numFilesUnzipped: Integer;

begin
  success := False;

  zip := TZip.Create;

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

  //  ------------------------------------------------------------
  //  Extract all files to:
  //  
  //      c:/temp/testDir
  //  
  //  If the target directory does not already exist,
  //  Chilkat will automatically create it.
  //  
  //  The Unzip method recreates the stored ZIP directory
  //  structure beneath the target directory.
  //  
  numFilesUnzipped := zip.Unzip('c:/temp/testDir');

  if (numFilesUnzipped < 0) then
    begin
      WriteLn(zip.LastErrorText);
      Exit;
    end;

  WriteLn('Number of files extracted = ' + numFilesUnzipped);
  WriteLn('');

  //  ------------------------------------------------------------
  //  Suppose the ZIP archive contains these entries:
  //  
  //      aaa/pigs.json
  //      bbb/base64Cert.txt
  //      bbb/sub1/brasil_cert.pem
  //      bbb/sub2/penguins.gif
  //      bbb/sub2/starfish.jpg
  //      hamlet.xml
  //      hello.pdf
  //  
  //  After extraction, the following files will exist:
  //  
  //      c:/temp/testDir/aaa/pigs.json
  //      c:/temp/testDir/bbb/base64Cert.txt
  //      c:/temp/testDir/bbb/sub1/brasil_cert.pem
  //      c:/temp/testDir/bbb/sub2/penguins.gif
  //      c:/temp/testDir/bbb/sub2/starfish.jpg
  //      c:/temp/testDir/hamlet.xml
  //      c:/temp/testDir/hello.pdf
  //  
  //  The ZIP directory structure is preserved during extraction.

  //  Close the ZIP archive.
  zip.CloseZip();

  WriteLn('Unzip completed successfully.');


  zip.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.