Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pack file using 7z with Delphi and show percent done?

When I run this from command line:

7z a 1.zip J:\test.mp4

I can see how much in % is done. When I tried to run this from Delphi using CreateProcess and CreatePipe I got nothing until the file is packed. Then it displayed the final output from 7zip.

My code looks like this:

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;

I don't want to just create a ZIP archive- I know there are nicer ways to do this in Delphi. I want to interact with a console applications. Output from many console applications can be processed with the code I posted, but it fails with 7zip- that's why I ask about 7zip here. What is so special about 7zip that its output can't be captured properly? How to capture output from applications like this 7zip?

like image 583
Tom Avatar asked Nov 30 '25 16:11

Tom


1 Answers

You can take a look at the plugin made by progdigy

Progress bar

 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;
like image 195
Hugues Van Landeghem Avatar answered Dec 02 '25 05:12

Hugues Van Landeghem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!