I'm trying to use Abbrevia to build a ZIP archive. The code looks like this:
procedure TMyClass.AddToArchive(archive: TAbZipArchive; const filename: string);
var
fullname: string;
begin
FReport.newStep(format('Preparing %s...', [filename]));
if trim(filename) = '' then
Exit;
fullname := TPath.Combine(GetRootPath(), filename);
if fileExists(fullname) then
archive.AddFiles(filename, faAnyFile)
else FMissingValues.add(ExtractFileName(fullname));
end;
procedure TMyClass.ZipProc(Sender : TObject; Item : TAbArchiveItem;
OutStream : TStream);
begin
AbZip(TAbZipArchive(Sender), TAbZipItem(Item), OutStream);
end;
procedure TMyClass.BuildArchive(const files, zipname: string);
var
list: TStringList;
archive: TAbZipArchive;
filename, root: string;
begin
archive := TAbZipArchive.Create(zipname, fmCreate);
list := TStringList.Create;
try
archive.InsertHelper := ZipProc;
root := GetRootPath();
archive.BaseDirectory := root;
list.Text := files;
for filename in list do
AddToArchive(archive, TPath.Combine(root, filename));
archive.Save;
finally
archive.Free;
list.free;
end;
end;
I get back a valid zipfile, except for one problem. In the resulting zipfile, the folder structure is created relative to the root of the C: drive, not relative to archive.BaseDirectory
. (Everything gets stored in there under \Users\Mason\Documents\etc...) So apparently I'm misunderstanding the purpose of the BaseDirectory
property. How do I get the files I insert to be stored relative to a specific root folder?
You shouldn't use the full path with AddFiles, only the relative path to BaseDirectory.