2014-02-06 29 views
1

我想使用Abbrevia建立一個ZIP檔案。代碼如下所示:如何在Abbrevia zip存檔中設置基線文件夾?

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; 

我找回了一個有效的zipfile,除了一個問題。在生成的zip文件中,文件夾結構是相對於C:驅動器的根目錄創建的,而不是相對於archive.BaseDirectory。 (所有內容都存儲在\ Users \ Mason \ Documents \下......)顯然,我誤解了BaseDirectory屬性的用途。如何獲取我插入的文件以相對於特定的根文件夾進行存儲?

+0

GetRootPath返回什麼? –

+0

@UweRaabe:我的文檔中的文件夾。 –

回答

3

您不應該使用AddFiles的完整路徑,只有BaseDirectory的相對路徑。

+0

我試過這樣做,沒有任何東西被添加。 –

+1

自從我使用Abbrevia以來,已經有很長的一段時間了,但這是它的工作方式。也許是後面的反斜槓(缺少或太多)?您的Abbrevia代碼中仍然有可能存在錯誤。 –

+0

這是問題所在。有一個*引導*反斜槓。刪除它使其工作。 –

相關問題