0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

TurboPower Abbrevia を使用して暗号化されたzipファイルを処理する

Last updated at Posted at 2017-02-22

環境

  • RadStudio XE8
  • TurboPower Abbrevia 5.2

暗号化されているか確認

TAbUnZipper.Itemsの返すTAbZipItemのIsEncrypted プロパティを調べる。

function CheckEncryption(const AFileName: string): Boolean;
var
  Zip: TAbUnZipper;
begin
  Zip := TAbUnZipper.Create(Nil);
  Zip.FileName := AFileName;
  Zip.TempDirectory := TPath.GetTempPath;
  Zip.ExtractOptions := [eoCreateDirs, eoRestorePath];

  Result := False;
  if Zip.Count > 0 then
  begin
    if Zip.Items[0].IsEncrypted then
      Result := True;
  end;
  Zip.Free;
end;

パスワードが正しいかチェック

TAbUnZipper.Passwordにパスワードを設定してTAbUnZipper.TestTaggedItemsを実行する。
パスワードが正しく無かった場合は例外としてEAbZipInvalidPasswordが投げられるのでそれで確認する。

function CheckPassword(const AFileName, APassword: string): Boolean;
var
  Zip: TAbUnZipper;
  OutPutPath: String;
begin
  Zip := TAbUnZipper.Create(Nil);
  Zip.FileName := AFileName;
  Zip.TempDirectory := TPath.GetTempPath;
  Zip.ExtractOptions := [eoCreateDirs, eoRestorePath];

  Zip.Password := APassword;
  Zip.TagItems('*.*');
  try
    Zip.TestTaggedItems;
    Result := True;
  except
    on EAbZipInvalidPassword do
    begin
      Result := False;
    end;
  end;
  Zip.Free;
end;

ライブラリにおまかせ

OnNeedPassword イベントでパスワードを設定する。

procedure TForm1.OnNeedPassword(Sender: TObject; var NewPassword: AnsiString);
var
  Val: String;
begin
  if InputQuery('パスワード入力', 'パスワードを入力してください', Val) then
    NewPassword := Val;
end;
procedure TForm1.ExctractFile(const AFileName: string);
var
  Zip: TAbUnZipper;
  OutPutPath: String;
  Pass: String;
begin
  Zip := TAbUnZipper.Create(Nil);
  Zip.FileName := AFileName;
  Zip.TempDirectory := TPath.GetTempPath;
  Zip.ExtractOptions := [eoCreateDirs, eoRestorePath];
  Zip.BaseDirectory := 'C:\';
  Zip.OnNeedPassword := OnNeedPassword;
  Zip.ExtractFiles('*.*');

  FreeAndNil(Zip);
end;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?