Recent

Author Topic: [SOLVED] Zipper Progress  (Read 3929 times)

pcurtis

  • Hero Member
  • *****
  • Posts: 951
[SOLVED] Zipper Progress
« on: December 13, 2020, 02:05:52 pm »
Hi All,

can any one see why the onprogress event does not fire?

The codes a mess, but I am testing

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ProgressHandler(Sender: TObject; const Percent: Double);
  2. begin
  3.   ProgressBar1.Position := round(Percent);
  4. end;
  5.  
  6. procedure TForm1.UNZIP_FILES;
  7. var
  8.   MyUnzip : TUnZipper;
  9.   MyFileList : TStrings;
  10.   iTEMP : integer;
  11.   sTEMP : string;
  12. begin
  13.   ProgressBar1.Max := 100;
  14.  
  15.   MyFileList := TStringList.Create;
  16.  
  17.   FindAllFiles(MyFileList, '.\files\', '*.zip', true);
  18.  
  19.   for iTEMP := 0 to MyFileList.Count - 1 do
  20.     begin
  21.       MyUnzip := TUnZipper.Create;
  22.       MyUnzip.Flat := false;
  23.  
  24.       MyUnzip.OnProgress := @ProgressHandler;
  25.  
  26.       StatusBar1.SimpleText := 'Unzipping - ' + MyFileList[iTEMP];
  27.  
  28.       MyUnzip.FileName := MyFileList[iTEMP];
  29.       sTEMP := Copy(MyFileList[iTEMP], 1, pos('_', MyFileList[iTEMP]) - 1);
  30.       Delete(sTEMP, 1, pos('\', sTEMP));
  31.       MyUnzip.OutputPath := 'd:\Media\Pictures\' + sTEMP + '\';
  32.  
  33.       try
  34.         MyUnzip.UnZip(MyFileList[iTEMP]);
  35.       except
  36.         ShowMessage('Corrupt zip file');
  37.       end;
  38.  
  39.       DeleteFile(MyFileList[iTEMP]);
  40.       FreeAndNil(MyUnzip);
  41.     end;
  42.  
  43.     MyFileList.Free;
  44. end;
  45.  

The code runs and actually does unzip the files, but no progress.

Thanks in advance.
« Last Edit: December 14, 2020, 01:50:58 am by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5407
  • My goal: build my own game engine using Lazarus
Re: Zipper Progress
« Reply #1 on: December 13, 2020, 02:24:31 pm »
I'm not very sure but maybe you need to call
Application.ProcessMessages; after changing the progress bar's position.

Paolo

  • Hero Member
  • *****
  • Posts: 581
Re: Zipper Progress
« Reply #2 on: December 13, 2020, 02:44:30 pm »
A call to
progressbar.update;
after position index increase should be enough.

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #3 on: December 13, 2020, 02:59:17 pm »
I have put a break point in the event handler and it does not get called.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

lainz

  • Hero Member
  • *****
  • Posts: 4703
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Zipper Progress
« Reply #4 on: December 13, 2020, 03:15:59 pm »
Please attach a compilable demo that shows the issue, is harder to play "guess the bug" without actually testing it.

bytebites

  • Hero Member
  • *****
  • Posts: 709
Re: Zipper Progress
« Reply #5 on: December 13, 2020, 03:19:48 pm »
Perhaps
Code: Pascal  [Select][+][-]
  1. MyUnzip.OnPercent := @ProgressHandler;

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #6 on: December 13, 2020, 03:58:32 pm »
? doesn't even compile the procedure declarations are different
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5407
  • My goal: build my own game engine using Lazarus
Re: Zipper Progress
« Reply #7 on: December 13, 2020, 04:19:43 pm »
Because you still haven't provided the source code, I can only eye-inspect what you already showed.

I believed you should remove:
  MyUnzip.OnProgress := @ProgressHandler;

but replace it with:
  ProgressBar1.Position := iTEMP;

Because your program is for unzipping multiple files and the progress bar's advancement can be handled directly inside the for-do loop in the TForm1.UNZIP_FILES procedure.

You may need to call Application.ProcessMessages; after changing the progress bar's position.
« Last Edit: December 13, 2020, 04:33:54 pm by Handoko »

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #8 on: December 13, 2020, 04:44:29 pm »
Sorry, I've just noticed the mistake in the source I originally pasted.

I have changed it. The progress bar should only be changed during the OnProgress event.

Adding ProgressBar1.Update; to the event handler helped, but now I see another problem.

Because, I believe, the unzip process runs in its own thread, the procedure finishes and only shows the progress from unzipping the last file. The logic I am looking for is

1. Build list of files to unzip
2. Start at beginning of list
3. Unzip file showing progress
4. If another zip file is in the list then select next file and repeat from step 3
5. End

Or is there another way?
« Last Edit: December 13, 2020, 04:53:55 pm by pcurtis »
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6877
Re: Zipper Progress
« Reply #9 on: December 13, 2020, 05:29:05 pm »
This most likely isn't your immediate problem but its not helping you..

You are creating MyUnZip  in the loop but you are not freeing it, at all actually..

That would be a starter  :)
The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #10 on: December 13, 2020, 06:03:58 pm »
Isnt FreeAndNil(MyUnzip) in the main loop?
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

wp

  • Hero Member
  • *****
  • Posts: 12770
Re: Zipper Progress
« Reply #11 on: December 13, 2020, 06:06:31 pm »
This is working for me (for a single zip file). Before running, please adjust the name of the zip file (const ZIP_FILENAME).

Code: Pascal  [Select][+][-]
  1. procedure TForm1.UnzipProgressExHandler(Sender : TObject; const ATotPos, ATotSize: Int64);
  2. begin
  3.   Progressbar1.Position := Round(ATotPos/ATotSize*100.0);
  4.   Memo1.Lines.Add(Format('ATotPos: %d, ATotSize: %d', [ATotPos, ATotSize]));
  5. end;
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. const
  9.   ZIP_FILENAME = 'D:\Fotos\Fotos.zip';
  10. var
  11.   myUnzipper: TUnzipper;
  12. begin
  13.   myUnzipper := TUnzipper.Create;
  14.   try
  15.     myUnzipper.OnProgressEx := @UnzipProgressExHandler;
  16.     myUnzipper.FileName := ZIP_FILENAME;
  17.     Progressbar1.Min := 0;
  18.     Progressbar1.Max := 100;
  19.     Progressbar1.Position := 0;
  20.     myUnzipper.UnzipAllFiles();
  21.   finally
  22.     myUnzipper.Free;
  23.   end;
  24. end;

jamie

  • Hero Member
  • *****
  • Posts: 6877
Re: Zipper Progress
« Reply #12 on: December 13, 2020, 06:09:22 pm »
Isnt FreeAndNil(MyUnzip) in the main loop?

oops, sorry , didn't see that..
The only true wisdom is knowing you know nothing

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #13 on: December 13, 2020, 06:19:00 pm »
No problem
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

pcurtis

  • Hero Member
  • *****
  • Posts: 951
Re: Zipper Progress
« Reply #14 on: December 13, 2020, 07:00:01 pm »
@wp - That's the thing it works for one zip file, but not more. It really sucks not having documentation / examples.
Windows 10 20H2
Laz 2.2.0
FPC 3.2.2

 

TinyPortal © 2005-2018