从Delphi使用7-Zip?

27

我想在Delphi中使用7-Zip DLL,但是一直没有找到合适的文档或示例。 有人知道如何在Delphi中使用7-Zip DLL吗?

7个回答

31

26

在Oliver Giesen的回答基础上进行拓展,和许多JEDI Code Library一样,我找不到任何好的文档,但是这个方法对我有效:

uses
   JclCompression;

procedure TfrmSevenZipTest.Button1Click(Sender: TObject);
const
   FILENAME = 'F:\temp\test.zip';
var
   archiveclass: TJclDecompressArchiveClass;
   archive: TJclDecompressArchive;
   item: TJclCompressionItem;
   s: String;
   i: Integer;
begin
   archiveclass := GetArchiveFormats.FindDecompressFormat(FILENAME);

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

   archive := archiveclass.Create(FILENAME);
   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;
end;

6

4

2
看起来Synopse不支持7zip,只有zip。 - hikari

3

链接已失效。但这个或许可以帮到你。http://docwiki.embarcadero.com/Libraries/XE2/en/System.Zip.TZipFile - EMBarbosa
2
不支持LZMA压缩的存档文件。 - kbickar

1

如果您只打算使用7Zip进行压缩和解压缩,请查看TZip组件。 我为了自己的需要编写了一个小包装器,您可以在Zipper.pas文件中找到并随意重用。


如果每个压缩对象都适合内存,TZip就可以正常工作。否则,你会有点为难。尝试制作一个300 MB的zip文件,然后使用TZip将这90个300 MB的zip文件压缩到另一个zip文件中,你会度过一段有趣的时光。 - Warren P

0

我尝试了许多解决方案,但都遇到了问题,这个解决方案有效。

下载https://github.com/zedalaye/d7zip,将7z.dll和sevenzip.pas复制到您的项目目录中,并将sevenzip.pas添加到您的项目中。

然后您就可以使用它来解压缩:

using sevenzip;

procedure Unzip7zFile (zipFullFname:string);
  var
    outDir:string;
  begin
    with CreateInArchive(CLSID_CFormat7z) do
    begin  
      OpenFile(zipFullFname);
      outDir := ChangeFileExt(zipFullFname, '');
      ForceDirectories (outDir);
      ExtractTo(outDir);
    end;
  end;

使用方法:

Unzip7zFile(ExtractFilePath(Application.ExeName) + 'STR_SI_FULL_1000420.7z');

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接