Page 1 of 1

TZipFile OnProgress

PostPosted: Tue Jun 02, 2020 7:11 pm
by theLizard
Wanting something to do I played around with TZipFile but cannot understand how to implement the OnProgress event, I have seen a delphi example https://stackoverflow.com/questions/51913186/delphi-10-2-how-to-use-the-zipprogress but I don't know anything about delphi, I can read and sort of understand but cannot convert it to the c++ equivilent.

It is probably a simple thing to do for experienced delphi coders, but, I am not one so, help me out if you can. :D

Re: TZipFile OnProgress

PostPosted: Wed Jun 03, 2020 11:52 am
by rlebeau
theLizard wrote:Wanting something to do I played around with TZipFile but cannot understand how to implement the OnProgress event, I have seen a delphi example https://stackoverflow.com/questions/51913186/delphi-10-2-how-to-use-the-zipprogress but I don't know anything about delphi, I can read and sort of understand but cannot convert it to the c++ equivilent.


Why not? What EXACTLY are you having trouble with during the conversion? What does your C++ code look like that is not working for you? Are you getting any errors with it? Please be more specific.

Re: TZipFile OnProgress

PostPosted: Wed Jun 03, 2020 4:24 pm
by theLizard
theLizard wrote:Why not? What EXACTLY are you having trouble with during the conversion?


Initially, before finding the delphi example, I assigned a method to the OnProgress event (as I thought this was the procedure), when adding a file via the Add(filename) which triggers the OnProgress event, I got access violations.

I am sure I am doing it all wrong but, going by the answer on the stack overflow response I thought that creating a component would be the object that allowed me to have access to the OnProgress event, I am doing something wrong.

Component header
Code: Select all
#ifndef TLZipH
#define TLZipH
// ---------------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
// ---------------------------------------------------------------------------------
#include <vcl.h>
#include <vector>
#include <windows.h>
#include "system.zip.hpp"
// ---------------------------------------------------------------------------------
class PACKAGE TLZip : public TComponent
   {
   typedef void __fastcall (__closure *TZipProgressEvent)(System::TObject* Sender, System::UnicodeString FileName, const TZipHeader &Header, __int64 Position);

   private:
      TZipProgressEvent FOnProgress;

      TZipFile* zip;

      UnicodeString fileName;
      TZipFile* __fastcall getZip();
   protected:

   public:
      __fastcall TLZip(TComponent* Owner);
      __fastcall ~TLZip();

      UnicodeString __fastcall getFileName();
      void __fastcall  setFileName(UnicodeString value );

   __published:
      __property UnicodeString FileName = { read = getFileName, write = setFileName };
      __property TZipProgressEvent Progress = {read=FOnProgress, write = FOnProgress};
      __property TZipFile* Zip = {read=getZip};

   };
// ---------------------------------------------------------------------------------
#endif


Component cpp
Code: Select all
#include <vcl.h>
#pragma hdrstop
// ---------------------------------------------------------------------------------
#include "TLZip.h"
#pragma package(smart_init)
// ---------------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
// ---------------------------------------------------------------------------------
static inline void ValidCtrCheck(TLZip *)
   {
   new TLZip(NULL);
   }
// ---------------------------------------------------------------------------------
// -----| TLZip |----------
// ---------------------------------------------------------------------------------
__fastcall TLZip::TLZip(TComponent* Owner) : TComponent(Owner)
   {
   zip = new TZipFile();
   //zip->OnProgress = &MyProgress;
   }
// ---------------------------------------------------------------------------------
__fastcall TLZip::~TLZip()
   {
   if(zip)
    delete zip;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getFileName() //public
   {
   return(fileName);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setFileName(UnicodeString value ) //public
   {
   if(fileName != value)
      fileName = value;
   }
// ---------------------------------------------------------------------------------
TZipFile* __fastcall TLZip::getZip()
   {
   return(zip);
   }
// ---------------------------------------------------------------------------------
namespace Tlzip
   {
   void __fastcall PACKAGE Register()
      {
      TComponentClass classes[1] = {__classid(TLZip)};
      RegisterComponents(L"bcbh", classes, 0);
      }
   }


form cpp where component was dropped.

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "TfrmZip.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TLAgent"
#pragma link "TLEdit"
#pragma link "TLDirDlg"
#pragma link "TLCheckListBox"
#pragma link "TLZip"
#pragma resource "*.dfm"
TfrmZip *frmZip;
unsigned short word;
//---------------------------------------------------------------------------
__fastcall TfrmZip::TfrmZip(TComponent* Owner)
   : TForm(Owner)
{


}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::Button1Click(TObject *Sender)
{
if(!dlg->Execute())
   return;
lb_Open->Clear();

for(int i=0; i < dlg->Files->Count; i++)
   {
   lb_Open->Items->Add(dlg->Files->Strings[i]);
   }

   if(lb_Open->Items->Count > 0)
   lb_Open->CheckAll(1, false, false);
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::Button2Click(TObject *Sender)
   {
   if(!dlgSave->Execute())
      return;


   zipit->Zip->Open(dlgSave->FileName, zmWrite);

   for(int i = 0; i < lb_Open->Items->Count; i++)
      {
      zipit->Zip->Add(lb_Open->Items->Strings[i]);
      }
   }
//---------------------------------------------------------------------------
void __fastcall TfrmZip::zipitProgress(TObject *Sender, UnicodeString FileName, const TZipHeader &Header,
               __int64 Position)
{
pb->Position = Position;
}
//---------------------------------------------------------------------------



Adding files to a zip is not an issue, the problem is Add(...) does not go into zipitProgress

I do not know where and how I need to work in the OnProgress event so that it works.

TIA Remy

Re: TZipFile OnProgress

PostPosted: Wed Jun 03, 2020 5:33 pm
by rlebeau
theLizard wrote:Initially, before finding the delphi example, I assigned a method to the OnProgress event (as I thought this was the procedure), when adding a file via the Add(filename) which triggers the OnProgress event, I got access violations.


Then please show THAT code, and indicate WHERE the AccessViolation is occuring.

theLizard wrote:I am sure I am doing it all wrong but, going by the answer on the stack overflow response I thought that creating a component would be the object that allowed me to have access to the OnProgress event, I am doing something wrong.


You DO NOT need to make a new component just to use the OnProgress event. And the code in the SO post you linked to does not make a whole new component, just a small utility class, which you can also do in C++, eg:

Code: Select all
class TMyZipProgress : public TObject
{
public:
    __classmethod void __fastcall ShowZipProgress(TObject *Sender, String FileName, const TZipHeader &Header , __int64 Position);
};

void __fastcall TMyZipProgress::ShowZipProgress(TObject *Sender, String FileName, const TZipHeader &Header , __int64 Position)
{
   // Show progress as needed...
}

...

TZipFile *FZip = new TZipFile;
FZip->OnProgress = &TMyZipProgress::ShowZipProgess;
...
delete FZip;


But, if you want to use your own component, then try something more like this:

Component header:

Code: Select all
#ifndef TLZipH
#define TLZipH
// ---------------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <System.Zip.hpp>
// ---------------------------------------------------------------------------------
class PACKAGE TLZip : public TComponent
{
private:
   TZipProgressEvent FOnProgress;
   TZipFile* FZip;
   UnicodeString FFileName;

   void __fastcall MyProgress(TObject* ASender, UnicodeString AFileName, const TZipHeader &AHeader, __int64 APosition);

public:
   __fastcall TLZip(TComponent* Owner);
   __fastcall ~TLZip();

   __property TZipFile* Zip = {read=FZip};

__published:
   __property UnicodeString FileName = {read=FFileName, write=FFileName};
   __property TZipProgressEvent OnProgress = {read=FOnProgress, write=FOnProgress};
};
// ---------------------------------------------------------------------------------
#endif


Component cpp:

Code: Select all
#include <vcl.h>
#pragma hdrstop
// ---------------------------------------------------------------------------------
#include "TLZip.h"
#include <System.SysUtils.hpp>
#pragma package(smart_init)
// ---------------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
// ---------------------------------------------------------------------------------
static inline void ValidCtrCheck(TLZip *)
{
   new TLZip(NULL);
}
// ---------------------------------------------------------------------------------
// -----| TLZip |----------
// ---------------------------------------------------------------------------------
__fastcall TLZip::TLZip(TComponent* Owner) : TComponent(Owner)
{
   FZip = new TZipFile();
   FZip->OnProgress = &MyProgress;
}
// ---------------------------------------------------------------------------------
__fastcall TLZip::~TLZip()
{
   delete FZip;
}
// ---------------------------------------------------------------------------------
void __fastcall TLZip::MyProgress(TObject* ASender, UnicodeString AFileName,
   const TZipHeader &AHeader, __int64 APosition)
{
   if (FOnProgress)
      FOnProgress(this, AFileName, AHeader, APosition);
}
// ---------------------------------------------------------------------------------
namespace Tlzip
{
   void __fastcall PACKAGE Register()
   {
      TComponentClass classes[1] = {__classid(TLZip)};
      RegisterComponents(L"bcbh", classes, 0);
   }
}


Form cpp:

Code: Select all
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "TfrmZip.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TLAgent"
#pragma link "TLEdit"
#pragma link "TLDirDlg"
#pragma link "TLCheckListBox"
#pragma link "TLZip"
#pragma resource "*.dfm"
TfrmZip *frmZip;
unsigned short word;
//---------------------------------------------------------------------------
__fastcall TfrmZip::TfrmZip(TComponent* Owner)
   : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::Button1Click(TObject *Sender)
{
   if (!dlg->Execute())
      return;
   lb_Open->Clear();

   for(int i=0; i < dlg->Files->Count; ++i)
   {
      lb_Open->Items->Add(dlg->Files->Strings[i]);
   }

   if (lb_Open->Items->Count > 0)
      lb_Open->CheckAll(1, false, false);
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::Button2Click(TObject *Sender)
{
   if (!dlgSave->Execute())
      return;

   zipit->Zip->Open(dlgSave->FileName, zmWrite);

   for(int i = 0; i < lb_Open->Items->Count; ++i)
   {
      zipit->Zip->Add(lb_Open->Items->Strings[i]);
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::zipitProgress(TObject *Sender, UnicodeString FileName,
   const TZipHeader &Header, __int64 Position)
{
   pb->Position = Position;
}
//---------------------------------------------------------------------------

Re: TZipFile OnProgress

PostPosted: Wed Jun 03, 2020 6:47 pm
by theLizard
rlebeau wrote:Then please show THAT code, and indicate WHERE the AccessViolation is occuring.


I don't have that code anymore but the code threw the error after assigning an event handler to the OnProgress event and as a file was being added to the zip, the access violation was triggered at the point where OnProgress event was triggered by the TZipFile->Add(file).

rlebeau wrote:You DO NOT need to make a new component just to use the OnProgress event.


Yes, I am well aware of that, I am making it a component for a learning exercise and convinience if ever I need to include a zip function in apps.

rlebeau wrote:But, if you want to use your own component, then try something more like this:


Looks like I was close, will modify my code and report back.

Cheers.

Re: TZipFile OnProgress

PostPosted: Wed Jun 03, 2020 7:13 pm
by theLizard
Ok, your suggestions worked, I was sooooo close.

In the end, I did not need the __closure

I needed to add
Code: Select all
void __fastcall MyProgress(TObject* ASender, UnicodeString AFileName, const TZipHeader &AHeader, __int64 APosition);


in private: section of header file.

I needed to reinstate zip->OnProgress = &MyProgress; after commenting it out in the cpp.

I needed to include the MyProgress method which, I did have at one point but did not do it correctly, I did not have the
Code: Select all
    if (FOnProgress)
         FOnProgress(this, AFileName, AHeader, APosition);


bit, but I think I did try it at one stage somewhere else, oh well, at least there are no access violations (yet) :P

Thanks Remy.

Re: TZipFile OnProgress

PostPosted: Sat Jun 13, 2020 10:29 pm
by theLizard
For what it is worth, and should be obvious that it is work in progress, but here is the code for the component (nowhere near complete I may add)

The code incorporates the functionality to call up a file dialog to select files for compression or select a folder containing the files to be compressed.

as an example

Code: Select all
   if(!zipit->FileExplorerShow(0, zipit->ZipPath.w_str() /* or zipit->InitFolder */, false))
      return;



I am sure, as always, that things can be done better, hope it can be useful or improved on (would not be hard to do :D ).

cpp
Code: Select all
#include <vcl.h>
#pragma hdrstop
// ---------------------------------------------------------------------------------
#include "TLZip.h"
#pragma package(smart_init)
// ---------------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
// ---------------------------------------------------------------------------------
static inline void ValidCtrCheck(TLZip *)
   {
   new TLZip(NULL);
   }
// ---------------------------------------------------------------------------------
// -----| TLZip |----------
// ---------------------------------------------------------------------------------
__fastcall TLZip::TLZip(TComponent* Owner) : TComponent(Owner)
   {
   FZip = new TZipFile();
   FZip->OnProgress = &MyProgress;
      /*
      FZipFileName;
      FZipPath;
      FZipInitDrive;
      FZipDestination;
      FInitFolder;
    */
   }
// ---------------------------------------------------------------------------------
__fastcall TLZip::~TLZip()
   {
   if(FZip)
      delete FZip;

   }
// ---------------------------------------------------------------------------------

// ---------------------------------------------------------------------------------
bool __fastcall TLZip::FileExplorerShow(HWND Owner, wchar_t *path, bool dirsOnly )
   {
   success = false;
   wchar_t* g_path;
      IShellItem *shellItem;
      HRESULT result = ::SHCreateItemFromParsingName(path,0,IID_IShellItem,reinterpret_cast<void**>(&shellItem));

      if(dirsOnly)
         FdialogOptions = FOS_PICKFOLDERS;
      else
         FdialogOptions = FOS_ALLOWMULTISELECT;

      IFileOpenDialog *pfd;

      if (SUCCEEDED(CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pfd))))
         {
         if (SUCCEEDED(result) && path != NULL )
            pfd->SetFolder(shellItem);

         DWORD dwOptions;
         if (SUCCEEDED(pfd->GetOptions(&dwOptions)))
            {
            pfd->SetOptions(dwOptions | FdialogOptions /*FOS_PICKFOLDERS*/);
            }
         if (SUCCEEDED(pfd->Show(NULL)))
            {
            IShellItemArray *psa;

            if(SUCCEEDED(pfd->GetResults(&psa)))
               {

               if (FAILED (hr))
                     MessageBoxW(0, L"Can't get FileOpenDialog Results: %s", L"Oops", 0);

                  hr = psa->GetCount(&count);

                  if (FAILED (hr))
                     MessageBoxW(0, L"Can't get FileOpenDialog Results Count: %s", L"Oops", 0);

                  if(count > 0)
                     success = true;

                  for (int i = 0; i < (int)count; i++)
                     {
                     IShellItem *item;
                     hr = psa->GetItemAt(i, &item);

                     if (FAILED (hr))
                        MessageBoxW(0, L"Can't get FileOpenDialog GetItemAt: %s", L"Oops", 0);

                     hr = item->GetDisplayName(SIGDN_FILESYSPATH, &g_path);

                     if (FAILED (hr))
                        MessageBoxW(0, L"Can't get FileOpenDialog GetDisplayName: %s", L"Oops", 0);


                     if(dirsOnly && SUCCEEDED (hr)) // this applies only if call was made to get folder name for
                        {                            // compressing all files in source folder
                        FSelectedFolder = g_path;
                        success = true;
                        break;
                        }

                     if(SUCCEEDED (hr))
                        {
                        file = new TFileInfo();
                        file->FileInfoPath = ExtractFilePath(g_path);
                        file->FileInfoName = ExtractFileName(g_path);
                        file->FullFilePath = g_path;
                        filelist.push_back(file);
                        }

                     item->Release();

                     }
                  psa->Release();
               }
            }
         if(pfd)
            pfd->Release();

         }
   return(success);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::MyProgress(TObject* ASender, UnicodeString AFileName, const TZipHeader &AHeader, __int64 APosition)
{
    if (FOnProgress)
         FOnProgress(this, AFileName, AHeader, APosition);
}
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getZipFileName()
   {
   return(FZipFileName);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setZipFileName(UnicodeString value)
   {
   if(FZipFileName != value)
      FZipFileName = value;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getZipPath()
   {
   return(FZipPath);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setZipPath(UnicodeString value)
   {
   if(FZipPath != value)
      FZipPath = value;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getZipInitDrive()
   {
   return(FZipInitDrive);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setZipInitDrive(UnicodeString value)
   {
   if(FZipInitDrive != value)
      FZipInitDrive = value;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getZipDestination()
   {
   return(FZipDestination);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setZipDestination(UnicodeString value)
   {
   if(FZipDestination != value)
      FZipDestination = value;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getInitFolder()
   {
   return(FInitFolder);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setInitFolder(UnicodeString value)
   {
   if(FInitFolder != value)
      FInitFolder = value;
   }
// ---------------------------------------------------------------------------------
UnicodeString __fastcall TLZip::getSelectedFolder()
   {
   return(FSelectedFolder);
   }
// ---------------------------------------------------------------------------------
void __fastcall TLZip::setSelectedFolder(UnicodeString value)
   {
   if(FSelectedFolder != value)
      FSelectedFolder = value;
   }
// ---------------------------------------------------------------------------------
namespace Tlzip
   {
   void __fastcall PACKAGE Register()
      {
      TComponentClass classes[1] = {__classid(TLZip)};
      RegisterComponents(L"bcbh", classes, 0);
      }
   }



.h
Code: Select all
#ifndef TLZipH
#define TLZipH
// ---------------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
// ---------------------------------------------------------------------------------
#include <vcl.h>
#include <vector>
#include <windows.h>
#include "system.zip.hpp"
#include "TLFileInfo.h"


// ---------------------------------------------------------------------------------
class PACKAGE TLZip : public TComponent
   {
   //enum ZipMode { zmClosed, zmRead, zmReadWrite, zmWrite };

   private:
      DWORD FdialogOptions, count;
      HRESULT hr;
      LPWSTR* name;
    TFileInfo* file;
      TZipProgressEvent FOnProgress;
      TZipMode FMode;
      TZipFile* FZip;

      UnicodeString FZipFileName;
      UnicodeString FZipPath;
      UnicodeString FZipInitDrive;
      UnicodeString FZipDestination;
      UnicodeString FInitFolder;
      UnicodeString FSelectedFolder;

      bool FZipDirectory;

      UnicodeString __fastcall getZipFileName();
      void __fastcall setZipFileName(UnicodeString value);
      UnicodeString __fastcall getZipPath();
      void __fastcall setZipPath(UnicodeString value);
      UnicodeString __fastcall getZipInitDrive();
      void __fastcall setZipInitDrive(UnicodeString value);
      UnicodeString __fastcall getZipDestination();
      void __fastcall setZipDestination(UnicodeString value);
      UnicodeString __fastcall getInitFolder();
      void __fastcall setInitFolder(UnicodeString value);

      UnicodeString __fastcall getSelectedFolder();
      void __fastcall setSelectedFolder(UnicodeString value);

      //      TZipFile* __fastcall getZip();
      void __fastcall MyProgress(TObject* ASender, UnicodeString AFileName, const TZipHeader &AHeader, __int64 APosition);
   protected:
      bool success;

   public:
      __fastcall TLZip(TComponent* Owner);
      __fastcall ~TLZip();

      bool __fastcall FileExplorerShow(HWND Owner, wchar_t *path, bool dirsOnly );
      std::vector<TFileInfo*> filelist;

   __published:
      __property TZipProgressEvent Progress = {read=FOnProgress, write = FOnProgress};
      __property TZipFile* Agent = {read=FZip};
      __property TZipMode Mode = {read=FMode, write=FMode};
      __property UnicodeString ZipFileName = {read=getZipFileName, write=setZipFileName};
      __property UnicodeString ZipPath = {read=getZipPath, write=setZipPath};
      __property UnicodeString ZipInitDrive = {read=getZipInitDrive, write=setZipInitDrive};
      __property UnicodeString ZipDestination = {read=getZipDestination, write=setZipDestination};
      __property UnicodeString InitFolder = {read=getInitFolder, write=setInitFolder};
      __property UnicodeString SelectedFolder = {read=getSelectedFolder, write=setSelectedFolder};

   };
// ---------------------------------------------------------------------------------
#endif


TFileInfo ->> for the vector of files to be compressed (reson I did this was to be able to show just the file name but have access to the full file path if need be)

.cpp
Code: Select all
//---------------------------------------------------------------------------

#pragma hdrstop

#include "TLFileInfo.h"
//---------------------------------------------------------------------------
__fastcall TFileInfo::TFileInfo()
   {

   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFullFilePath()
   {
   return(FFullFilePath );
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFullFilePath(UnicodeString value)
   {
   if(FFullFilePath != value)
      FFullFilePath = value;

   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFileInfoName()
   {
   return(FFileInfoName);
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFileInfoName(UnicodeString value)
   {
   if(FFileInfoName != value)
      FFileInfoName = value;
   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFileInfoPath()
   {
   return(FFileInfoPath);
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFileInfoPath(UnicodeString value)
   {
   if(FFileInfoPath != value)
    FFileInfoPath = value;
   }

#pragma package(smart_init)

.h

Code: Select all
//---------------------------------------------------------------------------

#pragma hdrstop

#include "TLFileInfo.h"
//---------------------------------------------------------------------------
__fastcall TFileInfo::TFileInfo()
   {

   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFullFilePath()
   {
   return(FFullFilePath );
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFullFilePath(UnicodeString value)
   {
   if(FFullFilePath != value)
      FFullFilePath = value;

   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFileInfoName()
   {
   return(FFileInfoName);
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFileInfoName(UnicodeString value)
   {
   if(FFileInfoName != value)
      FFileInfoName = value;
   }
//---------------------------------------------------------------------------
UnicodeString __fastcall TFileInfo::getFileInfoPath()
   {
   return(FFileInfoPath);
   }
//---------------------------------------------------------------------------
void __fastcall TFileInfo::setFileInfoPath(UnicodeString value)
   {
   if(FFileInfoPath != value)
    FFileInfoPath = value;
   }

#pragma package(smart_init)


For ref only,
Code: Select all
#include <vcl.h>
#pragma hdrstop

#include "TfrmZip.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TLAgent"
#pragma link "TLEdit"
#pragma link "TLDirDlg"
#pragma link "TLCheckListBox"
#pragma link "TLZip"
#pragma link "TLApplication"
#pragma resource "*.dfm"
TfrmZip *frmZip;
unsigned short word;
//---------------------------------------------------------------------------
__fastcall TfrmZip::TfrmZip(TComponent* Owner)
   : TForm(Owner)
{
/*
https://docs.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-ifileopendialog-getresults

*/
app->ApplicationPath = app->ExtractDebug(ExtractFilePath(Application->ExeName));

zipit->InitFolder = app->ApplicationPath;
zipit->ZipDestination = zipit->InitFolder;
zipit->ZipPath = ExtractFileDrive(zipit->InitFolder);
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::bnSelectFilesClick(TObject *Sender)
{
zipit->filelist.clear();
zipit->FileExplorerShow(0, zipit->InitFolder.w_str(), false);

if(zipit->filelist.size() > 0)
   lb_Open->Clear();

for(int i=0; i < zipit->filelist.size(); i++)
   {
   lb_Open->Items->Add( zipit->filelist[i]->FileInfoName);
   }

   if(lb_Open->Items->Count > 0)
   lb_Open->CheckAll(1, false, false);
}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::bnCompressClick(TObject *Sender)
   {
   if(!dlgSave->Execute())
      return;

   pb->Position = 0;

   zipit->Agent->Open(dlgSave->FileName, zmWrite);

   for(int i = 0; i < zipit->filelist.size(); i++)
      {
      pb->Position = 0;
      zipit->Agent->Add(zipit->filelist[i]->FullFilePath);
      Application->ProcessMessages();

      }
   zipit->Agent->Close();
   }
//---------------------------------------------------------------------------
void __fastcall TfrmZip::zipitProgress(TObject *Sender, UnicodeString FileName, const TZipHeader &Header,   __int64 Position)
   {
   pb->Position = Position;
   Application->ProcessMessages();
   sb->SimpleText = FileName;
   }
//---------------------------------------------------------------------------
void __fastcall TfrmZip::bnOpenZipClick(TObject *Sender)
{
//standard dialog for testing purpose

if(!dlg->Execute())
   return;

   zipit->Agent->Open(dlg->FileName, zmRead);
   if(zipit->Agent->FileCount > 0)
      {
      lb_Progress->Items->AddStrings(zipit->Agent->FileNames);
      }
   zipit->Agent->Close();

}
//---------------------------------------------------------------------------
void __fastcall TfrmZip::lb_OpenClick(TObject *Sender)
   {
   if(lb_Open->Items->Count > 0)
      {
      sb->SimpleText = zipit->filelist[lb_Open->ItemIndex]->FullFilePath;
      }
   }
//---------------------------------------------------------------------------
void __fastcall TfrmZip::bnExploreClick(TObject *Sender)
   {
   if(!zipit->FileExplorerShow(0, zipit->ZipPath.w_str(), false))
      return;
   }
//---------------------------------------------------------------------------

void __fastcall TfrmZip::bnSelectFolderClick(TObject *Sender)
{
zipit->FileExplorerShow(0, zipit->InitFolder.w_str(), true);
zipit->SelectedFolder;
}
//---------------------------------------------------------------------------


Re: TZipFile OnProgress

PostPosted: Sun Jun 14, 2020 8:04 pm
by HsiaLin
Seems you pasted the fileinfo cpp code to the h code too.

Re: TZipFile OnProgress

PostPosted: Tue Jun 16, 2020 3:54 pm
by theLizard
HsiaLin wrote:Seems you pasted the fileinfo cpp code to the h code too.


Oops, completely missed it :? here is the h code.

Code: Select all
//---------------------------------------------------------------------------

#ifndef TLFileInfoH
#define TLFileInfoH
#include <SysUtils.hpp>
#include <Classes.hpp>
// ---------------------------------------------------------------------------------
#include <StdCtrls.hpp>
#include <vector>
//---------------------------------------------------------------------------
class TFileInfo
   {
   private:
      UnicodeString FFileInfoName;
      UnicodeString FFileInfoPath;
      UnicodeString FFullFilePath;

      UnicodeString __fastcall getFullFilePath();
      void __fastcall setFullFilePath(UnicodeString value);
      UnicodeString __fastcall getFileInfoName();
      void __fastcall setFileInfoName(UnicodeString value);
      UnicodeString __fastcall getFileInfoPath();
      void __fastcall setFileInfoPath(UnicodeString value);


   public:
      __fastcall TFileInfo();

   __property UnicodeString FullFilePath = {read=getFullFilePath, write = setFullFilePath};
   __property UnicodeString FileInfoName = {read=getFileInfoName, write = setFileInfoName};
   __property UnicodeString FileInfoPath = {read=getFileInfoPath, write = setFileInfoPath};
   };
#endif