我有以下代码,总是因“抽象错误”而失败:

  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,比如给它 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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top