Почему я получаю “абстрактную ошибку” при вызове TJclCompressArchive.Сжимать?
-
21-09-2019 - |
Вопрос
У меня есть следующий код, который всегда завершается сбоем с "Абстрактной ошибкой".:
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;
Однако я всегда получаю эту ошибку при попытке сжатия.Есть какие-нибудь идеи о том, что я здесь делаю не так?
Решение
Я полагаю, что вы должны указать ему, какой тип JclCompressArchive создавать, например, give it arch := TJcl7zCompressArchive.Create...
вместо JclCompressArchive.Создайте().
Если вы посмотрите на раздел "Иерархия классов" 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;
Не связан с StackOverflow