//U N R A R.dll ------------------------------------------------------------------------------------
typedef RAROPENARCHIVEDATA   // struct RAROpenArchiveData
begin                        // {
  pointer ArcName;           //  char *ArcName;
  long OpenMode;             //  UINT OpenMode;
  long OpenResult;           //  UINT OpenResult;
  pointer CmtBuf;            //  char *CmtBuf;
  long CmtBufSize;           //  UINT CmtBufSize;
  long CmtSize;              //  UINT CmtSize;
  long CmtState;             //  UINT CmtState;
end;                         //};

typedef RARHEADERDATA         //struct RARHeaderData
begin                         //{
  string ArcName[260];        //  char ArcName[260];
  string FileName[260];       //  char FileName[260];
  long Flags;                 //  UINT Flags;
  long PackSize;              //  UINT PackSize;
  long UnpSize;               //  UINT UnpSize;
  long HostOS;                //  UINT HostOS;
  long FileCRC;               //  UINT FileCRC;
  long FileTime;              //  UINT FileTime;
  long UnpVer;                //  UINT UnpVer;
  long Method;                //  UINT Method;
  long FileAttr;              //  UINT FileAttr;
  pointer CmtBuf;             //  char *CmtBuf;
  long CmtBufSize;            //  UINT CmtBufSize;
  long CmtSize;               //  UINT CmtSize;
  long CmtState;              //  UINT CmtState;
end;                          //};

//HANDLE PASCAL RAROpenArchive(struct RAROpenArchiveData *ArchiveData)
prototype long unrar.RAROpenArchive      (RAROPENARCHIVEDATA POINTER);
//int PASCAL RARReadHeader(HANDLE hArcData, struct RARHeaderData *HeaderData)
prototype long unrar.RARReadHeader       (long, RARHEADERDATA POINTER);
//int PASCAL RARProcessFile(HANDLE hArcData, int Operation, char *DestPath, char *DestName)
prototype long unrar.RARProcessFile      (long, long, POINTER,POINTER);
//int PASCAL RARCloseArchive(HANDLE hArcData)
prototype long unrar.RARCloseArchive     (long);

//Funktionsaufrufe Unrar
prototype RAR_Extract(string,string,string,string);

//Defines
#define ERAR_END_ARCHIVE     10
#define ERAR_NO_MEMORY       11
#define ERAR_BAD_DATA        12
#define ERAR_BAD_ARCHIVE     13
#define ERAR_UNKNOWN_FORMAT  14
#define ERAR_EOPEN           15
#define ERAR_ECREATE         16
#define ERAR_ECLOSE          17
#define ERAR_EREAD           18
#define ERAR_EWRITE          19
#define ERAR_SMALL_BUF       20
#define ERAR_UNKNOWN         21

#define RAR_OM_LIST           0
#define RAR_OM_EXTRACT        1

#define RAR_SKIP              0
#define RAR_TEST              1
#define RAR_EXTRACT           2

//Einbinden DLL

 glbRarDllDir = SUPPORTDIR + "unrar.dll";     
 UseDLL(glbRarDllDir);


function RAR_Extract(svInst,svDest,svFile,svFilter)
    number                      nResult,iStatus;
    string                      svResult,svRarFile;
    long                        lHandle;
    RAROPENARCHIVEDATA          uRAR;               
    RARHEADERDATA               uHeader;
begin
    StrRemoveLastSlash(svInst);
    StrRemoveLastSlash(svDest);
    svRarFile = svInst + "\\" + svFile;
    if (FindFile(svInst,svFile,svResult) < 0) then
       WriteInstFile("RAR_OPEN_ERROR: " + "File not found" + " : " + svRarFile); //eigene Funktion
       return -1;
    endif;
    //Zuordung zur Struktur
    uRAR.ArcName = &svRarFile; 
    uRAR.OpenMode= RAR_OM_EXTRACT;
    uRAR.CmtSize = 0;
    //Handle ermitteln
    lHandle = unrar.RAROpenArchive (&uRAR);
    //ffnungsstatus
    nResult = uRAR.OpenResult;
    if (nResult != 0) then
      SetDialogTitle(DLG_MSG_SEVERE,"RAROpenArchive-Error");
      switch (nResult)
        case ERAR_NO_MEMORY:   
             WriteInstFile("RAR_OPEN_ERROR: " + "Not enough memory to initialize data structures"+ " : " + svRarFile);
             //nResult = MessageBox("Not enough memory to initialize data structures",SEVERE);
        case ERAR_BAD_DATA:    
             WriteInstFile("RAR_OPEN_ERROR: " + "Archive header broken"+ " : " + svRarFile);
             //nResult = MessageBox("Archive header broken",SEVERE);      
        case ERAR_BAD_ARCHIVE: 
             WriteInstFile("RAR_OPEN_ERROR: " + "File is not valid RAR archive"+ " : " + svRarFile);
             //nResult = MessageBox("File is not valid RAR archive",SEVERE);            
        case ERAR_EOPEN:       
             WriteInstFile("RAR_OPEN_ERROR: " + "File open error"+ " : " + svRarFile);
             //nResult = MessageBox("File open error",SEVERE);
      endswitch;
      return -1;
    endif;  
    //Archiv entpacken 1.FileHeader auslesen
    iStatus = unrar.RARReadHeader(lHandle, &uHeader);
    while (iStatus != ERAR_END_ARCHIVE)
       //Dateiname in Rar Datei 
       svResult = uHeader.FileName;
       nResult = Check_FileFilter(svResult,svFilter);
       if ( nResult = 0 ) then
          SetStatusWindow ( curProgBar, @MSG_EXTRACT + " " + glbSWText + svResult);
          //SdShowMsg(@MSG_EXTRACT + " "  + svResult,TRUE);
          nResult = unrar.RARProcessFile(lHandle,RAR_EXTRACT,&svDest,NULL);
          //SdShowMsg(@MSG_EXTRACT + " " + svResult,FALSE);
       else
          nResult = unrar.RARProcessFile(lHandle,RAR_TEST,&svDest,NULL);
       endif;   
       //Returnwert auswerten
       if (nResult != 0) then
         SetDialogTitle(DLG_MSG_SEVERE,"RAROpenArchive-Error");
           switch (nResult)
             case ERAR_BAD_DATA:   
                WriteInstFile("RAR_PROCESS_ERROR: " + "File CRC error"+ " : " + svRarFile);
                //nResult = MessageBox("File CRC error",SEVERE);
             case ERAR_BAD_ARCHIVE:    
                WriteInstFile("RAR_PROCESS_ERROR: " + "Volume is not valid RAR archive"+ " : " + svRarFile);
                //nResult = MessageBox("Volume is not valid RAR archive",SEVERE);      
             case ERAR_UNKNOWN_FORMAT: 
                WriteInstFile("RAR_PROCESS_ERROR: " + "Unknown archive format"+ " : " + svRarFile);
                //nResult = MessageBox("Unknown archive format",SEVERE);            
             case ERAR_EOPEN:       
                WriteInstFile("RAR_PROCESS_ERROR: " + "Volume open error"+ " : " + svRarFile);
                //nResult = MessageBox("Volume open error",SEVERE);
             case ERAR_ECREATE:   
                WriteInstFile("RAR_PROCESS_ERROR: " + "Volume open error"+ " : " + svRarFile);
                //nResult = MessageBox("Volume open error",SEVERE);
             case ERAR_ECLOSE:    
                WriteInstFile("RAR_PROCESS_ERROR: " + "File close error"+ " : " + svRarFile);
                //nResult = MessageBox("File close error",SEVERE);      
             case ERAR_EREAD: 
                WriteInstFile("RAR_PROCESS_ERROR: " + "Read error"+ " : " + svRarFile);
                //nResult = MessageBox("Read error",SEVERE);            
             case ERAR_EWRITE:       
                WriteInstFile("RAR_PROCESS_ERROR: " + "Write error"+ " : " + svRarFile);
                //nResult = MessageBox("Write error",SEVERE);
           endswitch;
           return -1;
       endif;  
       //Nchsten FileHeader auslesen
       iStatus = unrar.RARReadHeader(lHandle, &uHeader);
    endwhile; 
    nResult = unrar.RARCloseArchive (lHandle);
    SetStatusWindow ( curProgBar, "");
    return 0;
end;     

