我有以下代码总是因“抽象错误”而失败:
arch := TJclCompressArchive.Create(GetDesktop + 'Support.7z');
try
with arch do
begin
if FindFirst('*.log', faAnyFile, sr) = 0 then
begin
repeat
AddFile(ExtractFileName(sr.Name),sr.Name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
Compress; //this line throws the error
end;
finally
arch.free;
end;
但是,在尝试压缩时我总是遇到错误。关于我在这里做错了什么想法?
答案 0 :(得分:4)
我相信你必须告诉它要创建哪种JclCompressArchive,比如给它arch := TJcl7zCompressArchive.Create...
而不是JclCompressArchive.Create()。
如果查看JclCompression.pas的“类层次结构”部分:
TJclCompressionArchive
|
|-- TJclCompressArchive
| |
| |-- TJclSevenzipCompressArchive
| |
| |-- TJclZipCompressArchive handled by sevenzip ...
| |-- TJclBZ2CompressArchive handled by sevenzip ...
| |-- TJcl7zCompressArchive handled by sevenzip ...
| |-- TJclTarCompressArchive handled by sevenzip ...
| |-- TJclGZipCompressArchive handled by sevenzip ...
| |-- TJclXzCompressArchive handled by sevenzip ...
更新的
我认为使用StackOverflow的正确方法是添加一个新问题,因为在更新之后,这是一个完全不同的问题。
我不知道为什么你要将TJclCompressArchive转换为AddFile()和Compress(),它似乎对我没有强制转换
const
FILENAME = 'Support.7z';
var
archiveclass: TJCLUpdateArchiveClass;
arch: TJclUpdateArchive;
sr: TSearchRec;
begin
archiveclass := GetArchiveFormats.FindUpdateFormat(FILENAME);
if not Assigned(archiveclass) then
raise Exception.Create('Could not determine the Format of ' + FILENAME);
arch := archiveclass.Create(FILENAME);
try
// if FileExists(FILENAME) then // if you want to add any new files,
// arch.ListFiles; // in addition to what is already there
if FindFirst('*.pas', faAnyFile, sr) = 0 then
begin
repeat
arch.AddFile(ExtractFileName(sr.Name),sr.Name);
until FindNext(sr) <> 0;
FindClose(sr);
end;
arch.Compress;
finally
arch.free;
end;
end;