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
Bookmarks