1

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?

Stéphane B.
  • 3,260
  • 2
  • 30
  • 35
Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477

1 Answers1

3

You shouldn't use the full path with AddFiles, only the relative path to BaseDirectory.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
  • 1
    It has been a long time since I used Abbrevia, but that was the way it worked. Perhaps a trailing backslash (missing or too much)? It is still possible that there exists a bug in your Abbrevia code. – Uwe Raabe Feb 06 '14 at 16:31
  • That was the problem. There was a *leading* backslash. Removing that made it work. – Mason Wheeler Feb 06 '14 at 17:23