Ever wanted to make your own Zip Viewer? In this article we look at a simple code for listing the files/folders inside a ZIP file, without any third party libraries.
Compression softwares such as WinZIP, WinRAR, 7-zip has been one of the needed softwares for our PCs. We can compress dozens of files into a single file, also shrink its size at the same time, with these compression utilities. But what if we could make our own zip viewer?
We have looked into “How to create a .Zip File with your own program“, which was based on a third party library named ZipFile. Unfortunately the library could not open some zip files, though the add to zip functionality just works fine. So I had to find another alternative for “opening” a zip file. So I found paszlib
. It is available with the Lazarus installation, so there is no need to install any third party libraries.
So lets get on with a Quick Tutorial:
Quick Tutorial
Create a Application Project (Project -> New Project -> Application -> OK).
Drop a TFileNameEdit (from Misc tab), a TButton, TListBox, TLabel (all from Standard tab). You should create a form layout like the screenshot below:
Now set the Name
property of the TLabel to lblCount
. Also set the Filter
of TFileNameEdit to:
1 | ZIP Files (*.zip)|*.zip |
Switch to source code view (F12). Add Zipper
in the uses
clause:
1 | uses |
Now double click the Tbutton and enter:
1 | var |
Explanation:
1 | var |
We Initialize TUnZipper
(not the popular TZipper
type), because it can read the files inside the zip file. We set the Filename
that we want to read.
1 | szip.Examine; |
It checks the file for any defects and it reads the files and makes them available to our code.
1 | for i := 0 to szip.Entries.Count-1 do begin |
We iterate through all the file items/entries that are in the zip file and add them to our Listbox. We can also use many other information available to us instead of ArchiveFileName
. See the TZipFileEntry
class declaration below:
1 | TZipFileEntry = Class(TCollectionItem) |
We can also use, for example, Entries[i].IsDirectory
or Entries[i].Size
information to view extra information in our software. Hope to post another article with more options (such as adding files, extracting files etc.).
Now run the Project (F9 or Run -> Run). Open a .ZIP file and click the button. You will see a list of files and folders in the zip file.
Zipper (a.k.a. paszlib) can also be used in Console projects. So you can create command line programs with paszlib library.
Downloads
You can download the source code for the tutorial project and executable/EXE files from the links below: