如何在 Delphi 中使用 7z 打包文件并显示完成百分比?

Tom*_*Tom 3 delphi command-line pipe process

当我从命令行运行它时:

7z a 1.zip J:\test.mp4
Run Code Online (Sandbox Code Playgroud)

我可以看到完成了多少百分比。当我尝试使用 CreateProcess 和 CreatePipe 从 Delphi 运行它时,在文件打包之前我什么也没有。然后它显示了 7zip 的最终输出。

我的代码如下所示:

Stream:= THandleStream.Create(hRead);
try
  if not CreateProcess(nil, PChar(Cmd), nil, nil, 
                       True, 0, nil, nil, StartupInfo, 
                       ProcessInformation) then
    RaiseLastOSError;
  repeat
    if not GetExitCodeProcess(ProcessInformation.hProcess, ExitCode) then
      RaiseLastOSError;

    while Stream.Position < Stream.Size do 
    begin
      Stream.Read(C, 1);

      if (C = #13) then 
      begin
        Memo1.Lines.Add(S);
        S := '';
        Application.ProcessMessages;
      end
      else if C <> #10 then 
      begin
        S := S+C;
      end;
    end;
  until ExitCode <> Still_Active;
finally
  Stream.Free;
end;
Run Code Online (Sandbox Code Playgroud)

我不想只创建一个 ZIP 存档 - 我知道在 Delphi 中有更好的方法来做到这一点。我想与控制台应用程序进行交互。许多控制台应用程序的输出都可以使用我发布的代码进行处理,但是使用 7zip 却失败了——这就是我在这里询问 7zip 的原因。7zip 有什么特别之处以至于无法正确捕获其输出?如何从像 7zip 这样的应用程序中捕获输出?

Hug*_*hem 5

可以看看progdigy做的插件

进度条

 function ProgressCallback(sender: Pointer; total: boolean; value: int64): HRESULT; stdcall;
 begin
   if total then
     Mainform.ProgressBar.Max := value else
     Mainform.ProgressBar.Position := value;
   Result := S_OK;
 end;

 procedure TMainForm.ExtractClick(Sender: TObject);
 begin
   with CreateInArchive(CLSID_CFormatZip) do
   begin
     OpenFile('c:\test.zip');
     SetProgressCallback(nil, ProgressCallback);
     ...
   end;
 end;
Run Code Online (Sandbox Code Playgroud)