views:

292

answers:

1

I have the following code that always fails with an "Abstract Error":

  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;

However, I always get that error when trying to Compress. Any ideas on what I'm doing wrong here?

+3  A: 

I believe you have to tell it which kind of JclCompressArchive to create, such as give it arch := TJcl7zCompressArchive.Create... instead of JclCompressArchive.Create().

If you look at the "Class Hierarchy" section of 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 ...

Update
I think the proper way to use StackOverflow would have been to add a new question, since after the update, it's a completely different question.

I don't know why you're casting to TJclCompressArchive to AddFile() and Compress(), it seems to work for me without the casts

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
I think you're on the right track - please see my edit above.
croceldon
@croceldon: Your edit above does nothing to address the problem. See my comment to your original post.
Ken White
@jasonpenny: just tried your code - it works without errors, but all the files in the archive are zero size.
croceldon
It could be an issue with your JCL and 7-zip dll versions, I ran that code from Delphi2007, (copied 7z.dll from 7-zip version 4.65 in %ProgramFiles%\7-Zip\ to the EXE's directory and the latest JCL release), and on another computer from Delphi2009, (with 7z.dll from version 4.64 and the latest JCL,) and they both produced working .7z files.Or maybe something to do with the log files themselves? If you can right click them in Explorer and create the 7z file that way, I don't see why that code wouldn't work
jasonpenny