Results 1 to 1 of 1

Thread: Reading All RAR files

  1. #1

    Reading All RAR files

    Hi,

    I wanted to share with the community my way of using RAR files in C++ . RAR files offer better engryption and compression ratio than zip files.
    The solution need 3 files :
    -unrar.dll the original dll from UNRAR SDK
    -unrar.h a c++ interface for the dll (that i forked somehow)
    -Libunrar.a a static library for linking under mingw complilers (i dont like .lib linking and Visual Studio crap)

    How to use in your c++ project:
    - Plug the dll and the .h file into the same dir of your app.
    - Link with the .a library
    - Put the two functions below in your app

    Use the RGetfileEx function like this : unsigned char*Buffer=RGetfileEx("Rarfilename","extractfilename","password",size) , size must be int variable
    it searches for a specific file in the rar archive and decompresses it into the memory Buffer and returns the filesize in the variable size (4th parameter of the function)


    #include "unrar.h"
    
    unsigned char* BufferRAR=NULL;
    HANDLE rarFile;
    RARHeaderDataEx fileInfo;
    RAROpenArchiveDataEx archiveInfo;
    int cntp=0;
    int OldSize=0;
    
    int __stdcall CallbackProc(UINT msg,LONG UserData,LONG P1,LONG P2)
    {
      switch(msg)
      {
        case UCM_CHANGEVOLUME:
          return(0);
        case UCM_PROCESSDATA:
    		if (BufferRAR == NULL){
    			OldSize = 0;
    			cntp= 0;
    			BufferRAR = (unsigned char*) malloc(P2);
    			cntp=cntp+P2;
    			}
    		else
    			{
    			OldSize = cntp;
    			BufferRAR = (unsigned char*) realloc(BufferRAR, OldSize + P2);
    			cntp=cntp+P2;
    			}
    		memcpy(BufferRAR + OldSize,(unsigned char*)P1,P2);
    		return 1;
        case UCM_NEEDPASSWORD:
          return(0);
      }
      return(0);
    }
    
    
    unsigned char* RGetfileEx (char *fnm,char *rarnm,char *pasw,int &si){
         BufferRAR=NULL;
         HANDLE rarFile;
         RARHeaderDataEx fileInf;
         RAROpenArchiveDataEx archiveInf;
         memset(&archiveInf, 0, sizeof(archiveInf));
         archiveInf.CmtBuf = NULL;
         archiveInf.OpenMode = RAR_OM_EXTRACT;
         archiveInf.ArcName = rarnm;
         rarFile = RAROpenArchiveEx(&archiveInf);
    	 RARSetPassword(rarFile, pasw);
    	 RARSetCallback(rarFile, CallbackProc,NULL);
    
        if (archiveInf.OpenResult != 0) {
           RARCloseArchive(rarFile);
           cout  << "unrar couldn't open" << endl;
           return NULL;
        }
    	fileInf.CmtBuf = NULL;
    
    	int PFCode;
    	bool exi=FALSE;
    	si=0;
    	while (!exi){
    		//cout << fileInfo.FileName << " (" << fileInfo.UnpSize << ")" << endl;
    		if (stricmp(fileInf.FileName,fnm)==0){
                        PFCode = RARProcessFileW(rarFile, RAR_TEST, NULL, NULL);
    		    si=fileInf.UnpSize;
    		    if (PFCode != 0) cout  << "error processing this file\n" << endl;
    		    RARCloseArchive(rarFile);
    		    exi=TRUE;
    		}
    		else
    		{
    		PFCode=RARProcessFileW(rarFile, RAR_SKIP, NULL, NULL);
    		if (RARReadHeaderEx(rarFile, &fileInf)==ERAR_END_ARCHIVE)
    			{
    			exi=TRUE;
                            RARCloseArchive(rarFile);
    			cout << "end of archive:file not found" << endl;
    			BufferRAR=NULL;
    			}
    		}
    
    	}
    	return BufferRAR;
    }
    
    Hope that some one could integrate this in thinbasic modules.

    https://fileup.to/dxNN/unrar.h
    https://fileup.to/dxNM/libunrar.a
    https://fileup.to/dxNL/UnRAR.dll
    Last edited by lassad; 27-11-2022 at 08:32.

Similar Threads

  1. Saving and reading binary geometry files with TBGL
    By Petr Schreiber in forum TBGL Scripts and Projects
    Replies: 5
    Last Post: 31-07-2013, 17:38
  2. File Reading and separating "fields"
    By TheOne in forum File
    Replies: 5
    Last Post: 11-03-2011, 18:51
  3. Problem reading in keyboard data
    By GSAC3 in forum thinBasic General
    Replies: 3
    Last Post: 24-01-2009, 09:00
  4. Reading pixels?
    By Intio in forum thinBasic General
    Replies: 9
    Last Post: 26-09-2008, 21:05
  5. Question about File reading
    By matthew in forum File
    Replies: 7
    Last Post: 17-02-2008, 04:06

Members who have read this thread: 7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •