Delphi TZipMaster - how to use the Find method?
Using Delphi XE, TZipMaster 1.91 (latest)
I want to get the file name of first file name matching the FSpec arg that is passed to the Find function. However, I get an access violation when calling this function.
var
DBBakFl : String;
d : Integer;
begin
ZipMaster19开发者_StackOverflow1.ZipFileName := 'myzip.zip';
try
DBBakFl := ZipMaster191.Find('*.FBK', d).FileName;
except
raise Exception.Create('Find raised an exception');
end;
Any help appreciated.
EDIT: I sent an email got an almost immediate answer from one of the component's authors, Russell Peters. See my answer below.
Try something like:
var
DBBakFl : String;
d : Integer;
DirEntry: TZMDirEntry;
begin
ZipMaster191.ZipFileName := 'myzip.zip';
DirEntry := ZipMaster191.Find('*.FBK', d);
if Assigned(DirEntry) then
begin
DBBakF1 := DirEntry.FileName;
....
end;
As find is failing, what you are effectively trying is to get the file name from a nil TZMDirEntry. Basically the same as:
var
DBBakFl : String;
DirEntry: TZMDirEntry;
begin
DirEntry := nil;
DBBakF1 := DirEntry.FileName;
end;
If the call to Find fails, you don't get back a valid TZMDirEntry instance, so you can't access the FileName property.
Try assigning the result of Find to a variable and check it for validity before trying to access its properties or methods. Maybe something like this. The documentation shows that TZMDirEntry is an abstract class, so you might need to use a descendent class instead.
var
DBBakFl : String;
d : Integer;
lDirEntry: TZMDirEntry;
begin
ZipMaster191.ZipFileName := 'myzip.zip';
lDirEntry := ZipMaster191.Find('*.FBK', d);
if Assigned(lDirEntry) then
DBBakFl := lDirEntry.FileName
else
ShowMessage('file not found');
I sent an email got an almost immediate answer from one of the component's authors, Russell Peters:
I am not surprise you get an AV
var Idx: Integer; Entry: TZMDirEntry; DBBakFl : String; begin try Idx := -1; // search from beginning, starts at Idx + 1 Index := ZipMaster191.Find('*.FBK', Idx); if Index <> nil then DBBakFl := Index .FileName; except raise Exception.Create('Find raised an exception'); end; OR var Idx: Integer; DBBakFl : String; begin try Idx := -1; // search from beginning, starts at Idx + 1 if ZipMaster191.Find('*.FBK', Idx) <> nil then DBBakFl := ZipMaster191[Idx].FileName; except raise Exception.Create('Find raised an exception'); end; OR var Idx: Integer; DBBakFl : String; begin try Idx := -1; // search from beginning, starts at Idx + 1 ZipMaster191.Find('*.FBK', Idx) ; if Idx >= 0 then DBBakFl := ZipMaster191[Idx].FileName; except raise Exception.Create('Find raised an exception'); end; In a loop it is easy Idx := -1; while ZipMaster191.Find('*.FBK', Idx) <> nil do begin DBBakFl := ZipMaster191[Idx].FileName;
Russell Peters
精彩评论