【源码结构】
unit Form_Main;
interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls, ComCtrls;
type
TFormMain = class(TForm)
memoOutput: TMemo;
btnAddFiles: TButton;
btnCompress: TButton;
btnExtract: TButton;
OpenDialog: TOpenDialog;
ProgressBar: TProgressBar;
procedure btnAddFilesClick(Sender: TObject);
procedure btnCompressClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnExtractClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormMain: TFormMain;
implementation
{$R *.dfm}
uses sevenzip;
function ProgressCallback(Sender: Pointer; total: boolean; value: int64)
: HRESULT; stdcall;
begin
if total then
FormMain.ProgressBar.Max := value
else
FormMain.ProgressBar.Position := value;
Result := S_OK;
end;
procedure TFormMain.btnAddFilesClick(Sender: TObject);
var
Counter: Integer;
begin
if OpenDialog.Execute() then
begin
for Counter := 0 to OpenDialog.Files.Count - 1 do
begin
memoOutput.Lines.Append(OpenDialog.Files[Counter]);
end;
end;
end;
procedure TFormMain.btnCompressClick(Sender: TObject);
var
Arch: I7zOutArchive;
Counter: Integer;
begin
if OpenDialog.Files.Count = 0 then
begin
ShowMessage('请添加文件');
Exit;
end;
memoOutput.Lines.Append('开始压缩');
Arch := CreateOutArchive(CLSID_CFormat7z);
Arch.SetProgressCallback(nil, ProgressCallback);
for Counter := 0 to OpenDialog.Files.Count - 1 do
Arch.AddFile(OpenDialog.Files[Counter],
ExtractFileName(OpenDialog.Files[Counter]));
SetCompressionLevel(Arch, 5);
SevenZipSetCompressionMethod(Arch, m7Deflate64);
Arch.SaveToFile('.\test.7z');
memoOutput.Lines.Append('完成压缩');
Arch := nil;
end;
评论