Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 为什么调用TJclCompressArchive.Compress时会出现“抽象错误”?_Delphi_Compression_Abstract_7zip - Fatal编程技术网

Delphi 为什么调用TJclCompressArchive.Compress时会出现“抽象错误”?

Delphi 为什么调用TJclCompressArchive.Compress时会出现“抽象错误”?,delphi,compression,abstract,7zip,Delphi,Compression,Abstract,7zip,我有以下代码,它总是以抽象错误失败: 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);

我有以下代码,它总是以抽象错误失败:

  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.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来添加文件和压缩,如果没有强制转换,它似乎对我有效

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;

我相信您必须告诉它要创建哪种JclCompressArchive,例如give it 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来添加文件和压缩,如果没有强制转换,它似乎对我有效

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;

我认为你的思路是对的-请看我上面的编辑。@jasonpenny:刚刚尝试了你的代码-它可以正常工作,但存档中的所有文件都是零大小。这可能是你的JCL和7-zip dll版本的问题,我从Delphi2007运行了该代码,将%ProgramFiles%\7-zip\中的7-zip版本4.65中的7z.dll复制到EXE目录和最新的JCL版本,并在Delphi2009的另一台计算机上使用版本4.64和最新的JCL中的7z.dll复制到该目录,它们都生成了工作的.7z文件。或者可能与日志文件本身有关?如果您可以在资源管理器中右键单击它们并以这种方式创建7z文件,我不明白为什么该代码不起作用我认为您在正确的轨道上-请查看上面的编辑。@jasonpenny:刚刚尝试了您的代码-它可以正常工作,但存档中的所有文件都是零大小。这可能是您的JCL和7-zip dll版本的问题,我从Delphi2007运行了该代码,将%ProgramFiles%\7-zip\中的7-zip版本4.65中的7z.dll复制到EXE目录和最新的JCL版本,并在Delphi2009的另一台计算机上使用版本4.64和最新的JCL中的7z.dll,它们都生成了工作的.7z文件。或者可能与日志文件本身有关?如果您可以在资源管理器中右键单击它们并以这种方式创建7z文件,我不明白为什么该代码不会这样做work@Ken怀特-我只是根据你和jasonpenny的建议反映了我的编辑,但我的归档文件中仍然有零大小的文件…?Ken,更改变量声明不会影响任何事情。只要对象的运行时类型不是抽象的,变量的编译时类型就无关紧要。这就是虚拟方法的工作原理,抽象与否。欢迎来到堆栈溢出,Croceldon。杰森是对的。你的编辑完全改变了问题。你原来的问题已经回答了。请删除更新并将其作为新问题发布。然后回来接受Jason的回答。好的,Rob,我接受了你的建议。@Ken White-我只是反映了我对你和jasonpenny建议的内容的编辑,但我的存档中仍然有零大小的文件…?Ken,更改变量声明不会影响任何事情。只要对象的运行时类型不是抽象的,变量的编译时类型就无关紧要。这就是虚拟方法的工作原理,抽象与否。欢迎来到堆栈溢出,Croceldon。杰森是对的。你的编辑完全改变了问题。你原来的问题已经回答了。请删除更新并将其作为新问题发布。然后回来接受杰森的回答。好的,罗伯,我接受了你的建议。