Creating and extracting ZIP archives in Delphi

Since version XE2, Delphi is shipped with the built-in unit “System.Zip” for creating, editing and extracting ZIP compressed files. It is based on the RTL, and it can work slightly with VCL and FMX frameworks on all supported targets.

In this tutorial, we will show you what are the abilities of this unit and how to use it properly.

Unlike other compression librairies and components available for Delphi, this unit is completely written in Object Pascal : it doesn’t require any dependancies or external libraries.

This unit provides many ready-to-use features, you can use it to :

  • Extract all or specific files from a zip file.
  • Compress files and create a new zip file.
  • Add files to an existing zip file.
  • Access meta informations (name, comment, etc.) of the zip file and it contained files.
  • Tracking the progress of zip file tasks (compression, decompression, reading files) with event handler.
  • Access to the raw stream of a file within the zip archive (for decrypt purposes) using event before decompressing the file.

Basics

The functions (for creating, editing and extracting a ZIP archive’s contents) reside in the class “TZipFile”. Among these functions :

  • Add : Procedure for adding a file to the opened zip file.
  • Extract : Procedure for extracting one file for the opened zip file.
  • ExtractAll : Procedure for extracting all the content of the zip file.
  • ZipDirectoryContents : Class procedure for creating a zipped file from a folder’s contents.
  • IsValid : Class function for validating the zip file.
  • ExtractZipFile : Class procedure for extracting all the content of the zip file.

How to use

First, include the unit to the uses clauses :

uses
..., System.Zip, ...

To use the procedures, you need to create an instance of “TZipFile”.

Then, open a ZIP file, using the “open” procedure, by specifying the full FileName and the required Zip mode :

  • zmClosed : The file is closed for reading and writing.
  • zmRead : Open the file for reading only.
  • zmWrite : Open the file for writing only. Writing to the file will replace completely the current contents.
  • zmReadWrite : Open the file to modify the current contents rather than replacing them.
procedure ...
var
AZipFile : TZipFile;
begin
AZipFile := TZipFile.Create;
AZipFile.Open('MyFile.zip', zmRead);

Later, use the instruction (try … finally … end;) to perform tasks like extracting and adding files. Then, close the Zip file. And release the TZipFile instance on the end of the instruction.

...
  try
  //Here task specific code
  AZipFile.Close;   
  finally
  AZipFile.Free;
  end;
end;

And here are some ready-to-use code snippets that you can add to a method, or you can use them to create a quick class procedures.

Extract all the contents of the archive

You can use the object procedure :

procedure ...
var
AZipFile : TZipFile;
begin
AZipFile := TZipFile.Create;
AZipFile.Open('MyFile.zip', zmRead);      
  try
  AZipFile.ExtractAll(GetLibraryPath);
  AZipFile.Close;   
  finally   
  AZipFile.Free;  
  end;
end;

Or, you can use the quick class procedure :

TZipFile.ExtractZipFile('MyFile.zip'; '/Files/ArchiveFiles'; nil);

Extract a specific file from an archive

procedure ...
var
AZipFile : TZipFile;
begin
AZipFile := TZipFile.Create;
AZipFile.Open('MyFile.zip', zmRead);      
  try
  AZipFile.Extract('File1.dat', GetLibraryPath);
  AZipFile.Close;   
  finally   
  AZipFile.Free;  
  end;
end;

Create a ZIP file from a folder’s contents

TZipFile.ZipDirectoryContents('MyNewZipFile.zip', '/Files/FilesToZip', zcDeflate, nil);

Add a file to an existing ZIP file

procedure ...
var
AZipFile : TZipFile;
begin
AZipFile := TZipFile.Create;
AZipFile.Open('MyFile.zip', zmReadWrite);     
  try
  AZipFile.Add('File1.pdf', '', zcDeflate);
  AZipFile.Close;   
  finally   
  AZipFile.Free;  
  end;
end;

Validate a ZIP file then extract its contents (without object creation)

procedure ...
var
ZipFile : string;
begin
ZipFile := 'ZippedFolder.zip'; //This is my zip file
if TZipFile.IsValid(ZipFile) then //Validate zip file
  begin
  TZipFile.ExtractZipFile(ZipFile, 'C:\DestinationDirectory'); //Extract the zip file
  end
else
  begin
  ShowMessage('Your Zip File is not valid');
  end;
end;

See also

Leave a Reply