Creating a Class to Access RAR Archives
Peter Davies - GUI Computing
The Task:
Create a class to access the list of files contained in a RAR (similar to a ZIP)
archive in Delphi 2.0. Keep it generic enough so that other compression types can
be added in the future.
The Solution:
An approach, used back in the days of Borland Pascal Turbo Vision to tackle a
similar task, was to use the class TCollection to store a list of file objects.
Unfortunately, TCollection in Delphi has taken on a whole new look and does
not really suit the requirements. Instead, the class TList in Delphi is
better suited. It basically stores a list of objects of any type. The beauty
of TList, over the use of the conventional array, is that it dynamically grows
with the addition of new objects to the list - whereas an array must be set
to a finite size before a maximum size might be actually known.
TList also takes care of many of the tasks of maintaining a sequential list,
such as removing items in the middle of the list, adding items in the middle
of a list and so on.
Certain details need to be stored about each file in the RAR archive. To do this we will create a small class that stores the details and knows how to create and destroy itself. The class will be of the following structure:
TFileDetails = class
Filename : PChar;
CompressedSize : Longint;
OriginalSize : Longint;
FileDate : Longint;
constructor Create(AFileName : String;ACompressedSize : Longint;
AOriginalSize : Longint;AFileDate : Longint);
destructor Destroy; override;
end;
On creating an instance of TFileDetails, the information about the file (passed as parameters), is stored in the object. Many of these objects create all the information we need to know about the RAR archive. A class is required to handle the creating of each instance of TFileDetails Class. This is accomplished by the TArchive Class. This class consists of the following structure:
TArchive = class
private
FileName : PChar;
FileList : TList;
ArchiveOK : boolean;
ArchiveType : Longint;
TotOrigSize,
TotCompSize,
FileDate : Longint;
public
constructor Create(AFileName : String);
destructor Destroy; override;
function GetArchiveOk : boolean;
function GetTotalFiles : Integer;
function GetAFileName(Index : Integer) : String;
end;
This class requires the filename of the RAR archive as input. An example of using this class might be:
var
Archive : TArchive;
Counter : Integer;
begin
Archive := TArchive.Create('C:\TEST.RAR');
if Archive.GetArchiveOk then begin
For Counter := 1 to Archive.GetTotalFiles do begin
... do something
do something with the filename ... := Archive.GetAFileName(Counter)
... do something
end;
end;
Archive.Free;
end;
Some tips used in the creation of the source, which may help other Delphi developers, are:
Eg: FileList.Add(TFileDetails.Create(FName,FBlock.OrigSize,FBlock.PackSize,FBlock.FTime));
When obtaining information about a particular object, in the list of objects (Tlist), it is not necessary to actually assign the object being read to an object. You can simply tell Delphi the type of object it is (type cast) and then tell it the variable/etc you wish to get the information from.
Eg: sResult := StrPas(TFileDetails(FileList.Items[Index-1] ).FileName);
|
|
Here is the relevant code. In future AVDF articles, this class will be expanded upon, creating many ways of accessing the data in an instance of the class. This class provides the basis for a general purpose way of accessing RAR archives. |