ZipForge 和无效档案

发布于 2024-12-27 03:14:29 字数 725 浏览 0 评论 0原文

我有 ZipForge for Delphi XE2 &德尔福XE2。

我尝试测试任何无效的 zip 存档(例如未完全下载),就像在他们的演示中一样:

procedure TfmMain.bnStartClick(Sender: TObject);
begin
  with Archiver do
  begin
    FileName := 'c:\2.zip';
    OpenArchive;
    try
      TestFiles('*.*');
    except
      MessageDlg('Errors occurred in the archive file', mtError, [mbOk], 0);
    end;
    CloseArchive;
  end;
end;

但是我的异常没有触发; ZipForge 的对话框代替我的对话框被触发。

我尝试了 Abbrevia 组件 但它甚至无法识别存档是否无效...

请帮助我使我的例外工作(不是 ZipForge 的例外)或建议我一个更好的具有测试功能的 zip 文件组件。谢谢!

I have ZipForge for Delphi XE2 & Delphi XE2.

I try to test any invalid zip archives (e.g. not fully downloaded) like in their demo:

procedure TfmMain.bnStartClick(Sender: TObject);
begin
  with Archiver do
  begin
    FileName := 'c:\2.zip';
    OpenArchive;
    try
      TestFiles('*.*');
    except
      MessageDlg('Errors occurred in the archive file', mtError, [mbOk], 0);
    end;
    CloseArchive;
  end;
end;

But my exception doesn't fire; ZipForge's dialog fires instead of mine.

I tried Abbrevia Component but it even can't recognize if an archive is invalid...

Please help me to make my exception working (not ZipForge's one) or suggest me a better component for zip files with a test feature. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2025-01-03 03:14:29

请注意,您可以修改 ZIP 文件,例如通过稍微截断它们,ZIP 文件仍然有效。在我的测试文件中,我删除了最后 5000 字节,结果报告为有效。我使用 ZIP 程序成功提取了它。当然,提取的内容是错误的,不是原始内容。也许这就是发生在你身上的事情。也许您尝试损坏 ZIP 文件实际上并没有将其变成无效的 ZIP 文件。

Delphi XE2 附带了一个内置的 ZIP 组件,该组件在我的简单测试中运行良好,并在我截断文件足以使其真正损坏后成功检测到无效文件。

我使用 IsValid 方法检查有效性。这是我非常简单的测试程序。

program ZipTest;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Zip;

procedure Main;
const
  ZipArchive = 'C:\desktop\test.zip';
var
  ZipFile: TZipFile;
  FileName: string;
begin
  ZipFile := TZipFile.Create;
  try
    if ZipFile.IsValid(ZipArchive) then begin
      ZipFile.Open(ZipArchive, zmRead);
      for FileName in ZipFile.FileNames do begin
        Writeln(FileName);
      end;
    end else begin
      Writeln(ZipArchive + ' not valid');
    end;
  finally
    ZipFile.Free;
  end;
end;

begin
  try
    Main;
    Readln;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
    end;
  end;
end.

Be aware that you can modify ZIP files, e.g. by truncating them somewhat, the ZIP file will still be valid. With my test file, I removed the final 5000 bytes and it was reported as valid. I extracted it successfully using my ZIP program. Of course the extracted contents were incorrect and not the original contents. Perhaps this is what was happening for you. Maybe your attempts to corrupt your ZIP file were not in fact making it into an invalid ZIP file.

Delphi XE2 comes with a built in ZIP component that worked well in my simple test and successfully detected an invalid file, once I had truncated the file enough to make it truly corrupt.

I used the IsValid method to check validity. Here is my very simple test program.

program ZipTest;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Zip;

procedure Main;
const
  ZipArchive = 'C:\desktop\test.zip';
var
  ZipFile: TZipFile;
  FileName: string;
begin
  ZipFile := TZipFile.Create;
  try
    if ZipFile.IsValid(ZipArchive) then begin
      ZipFile.Open(ZipArchive, zmRead);
      for FileName in ZipFile.FileNames do begin
        Writeln(FileName);
      end;
    end else begin
      Writeln(ZipArchive + ' not valid');
    end;
  finally
    ZipFile.Free;
  end;
end;

begin
  try
    Main;
    Readln;
  except
    on E: Exception do begin
      Writeln(E.ClassName, ': ', E.Message);
    end;
  end;
end.
森罗 2025-01-03 03:14:29

如果您的 ZIP 文件无效,则对 OpenArchive 的调用很可能会失败。只要您的执行处理不涵盖这种情况,您就会得到您所描述的结果。

更新:在 TestFiles 或任何其他方法期间捕获异常的建议方法是连接 OnProcessFileFailure 事件处理程序。

If you have an invalid ZIP file, it is most likely that the call to OpenArchive will fail. As long as your execption handling doesn't cover that case, you will get the result you describe.

Update: The suggested way to catch exceptions during TestFiles or any other method is to connect an OnProcessFileFailure event handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文