0
votes

I'm trying to zip some files using Delphi 2007 using the JEDI JCL. The problem is I can't figure out why I keep getting this error "Sevenzip: Failed to load 7z.dll"

My code is :

var
   archiveclass: TJclDecompressArchiveClass;
   archive: TJclDecompressArchive;
   item: TJclCompressionItem;
   s: String;
   i: Integer;
begin
   archiveclass := GetArchiveFormats.FindDecompressFormat(dir);

   if not Assigned(archiveclass) then
      raise Exception.Create('Could not determine the Format of ' + dir);

   archive := archiveclass.Create(dir);
   try
      if not (archive is TJclSevenZipDecompressArchive) then
         raise Exception.Create('This format is not handled by 7z.dll');

      archive.ListFiles;

      s := Format('test.zip Item Count: %d'#13#10#13#10, [archive.ItemCount]);

      for i := 0 to archive.ItemCount - 1 do
      begin
         item := archive.Items[i];
         case item.Kind of
            ikFile:
               s := s + IntToStr(i+1) + ': ' + item.PackedName + #13#10;
            ikDirectory:
               s := s + IntToStr(i+1) + ': ' + item.PackedName + '\'#13#10;//'
         end;
      end;

      if archive.ItemCount > 0 then
      begin
//         archive.Items[0].Selected := true;
//         archive.ExtractSelected('F:\temp\test');

         archive.ExtractAll('F:\temp\test');
      end;

      ShowMessage(s);
   finally
      archive.Free;
   end;

I have the 7z.dll in the same folder as the Delphi project. What Am I doing wrong? Also is there any other simple way to 7z a folder? I'm not looking for some complex tasks, just to create a zip from a folder.

1
The executable doesn't really care where your project is. Is the DLL in the same directory as the executable? If the DLL can be found but not loaded then either it is the wrong bitness, or is missing dependencies. You can work it all out if you do some debugging.David Heffernan
My program breaks at ListFiles; and for some reason I can't set a breakpoint there... who can I debug it? I've tried about 5 ways to zip some files, it looks like an imposible task for me ^^.CiucaS
Find the code in the JCL which loads the DLL. Then add diagnostics to call GetProcAddress when it fails. Find out the error code. Also run under Dependency Viewer profile mode to find out why the DLL load fails.David Heffernan
How can I find that? THere is no 7z.dll or even dll string in the JCLCompresion unit where LoadFiles is . Also the last part is something unknown for me....CiucaS
It's going to be hard for us to help. You are lacking most of the knowledge needed to debug this. I see a long road ahead.David Heffernan

1 Answers

5
votes

The JCLCompression unit only wraps the 7z API inside JCLCompression classes. The 7z API itself resides in the SevenZip.pas unit (in the windows folder of the JCL source). This is where the 7z.dll is loaded (by the Load7Zip routine, when required).

You appear to be compiling the project with dynamic linking to that DLL, resulting in the DLL only being loaded when needed, rather than being loaded and linked with your EXE. The fact that the loading is failing and the error message you are seeing in the exception indicates some problem with finding or loading that DLL at runtime.

Things to check:

  • Ensure that the 7z.dll is in the same folder as your EXE (not the DPR source file, but the EXE at runtime)

  • Ensure that the 7z.dll you are using is 32-bit. Delphi 2007 produces 32-bit executables only, so even on a 64-bit OS you will still need the 32-bit version of 7z.dll for your Delphi application.