How to create a .Zip File with your own program

Zip files are a very common way of sharing multiple files compressed into a single file. Zip files are supported in various platforms (Windows, Mac, Linux etc.). We can use many softwares to create a zip file, such as 7-zip, WinZip, WinRAR etc. But today we are going to make zip files on our own program!

1. Download:

Download the ZipFile library from here: http://sourceforge.net/projects/lazarus-ccr/files/ZipFile/
Extract the zip file in the project directory (in which we will save the project later). You can use this as a package (lpk) in lazarus to drop it in a Form. But it will require us to rebuild Lazarus. We will use the second option. We will include the files in the project directory and call the functions using the library files sitting there.

2. Create a new project.

Start Lazarus.
Create a new project and click File -> Save All and save all the files to the project directory.

Go to Project -> Project Options -> Compiler Options -> Paths. In the Other unit files add the “zipfile” directory that you have extracted.

3. Now design the form.

Press F12 to switch to Form View. Drop 2 TFileNameEdits, 1 TButton in the form.

Double click the form and write the following code.

1
2
3
  FileNameEdit1.FileName:=Application.Location + 'test.txt';

  FileNameEdit2.FileName:=Application.Location + 'test.zip';

Here we set the default values of TFileEdits. Do not forget to create a test.txt file in the project directory, in order to test the program with the default filenames.

4. Button code.

Press F12 to switch to code view. Add the ZipFile unit to the uses section at the top of the unit file code. For example my uses clause has turned to be like this:

1
2
3
uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  EditBtn, ZipFile;

Double click the TButton and write the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var
  OurZipper: TZipFile;
begin
  // we CREATE the component for zipping
  OurZipper := TZipFile.Create(Form1);

  try
    // we specify the zip file name
    OurZipper.FileName := FileNameEdit2.FileName;

    // activate the component for use
    OurZipper.Activate;

    // we add
    OurZipper.AppendFileFromDisk(FileNameEdit1.FileName, ExtractFileName(FileNameEdit1.FileName));

  finally
    OurZipper.Free;

  end;

end;

5. Run the program

Press F9 or click Run -> Run to Run the program we have made.

Click the button. And then visit the project directory. You will see a “test.zip” file created by the program.

You can check here for instructions on how to use ZipFile as a package component to use in the form view:
http://wiki.freepascal.org/ZipFile

Downloads

You can download the source code for the tutorial project and executable/EXE files from the links below: