我想在Delphi中使用7-Zip DLL,但是一直没有找到合适的文档或示例。 有人知道如何在Delphi中使用7-Zip DLL吗?
在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;
无需DLL即可使用Zip和7z压缩格式,可以尝试使用Synopse: http://synopse.info/forum/viewtopic.php?pid=163
Delphi现在具有原生的跨平台zip支持,使用XE2中的TZipFile:
如果您只打算使用7Zip进行压缩和解压缩,请查看TZip组件。 我为了自己的需要编写了一个小包装器,您可以在Zipper.pas文件中找到并随意重用。
我尝试了许多解决方案,但都遇到了问题,这个解决方案有效。
下载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');