Click here to Skip to main content
15,063,784 members
Articles / Desktop Programming / MFC
Posted 3 Aug 2005


38 bookmarked

CUnrarDLL 1.0 - An MFC UnRAR class

Rate me:
Please Sign up or sign in to vote.
4.69/5 (11 votes)
9 Aug 20052 min read
An easy to use class based around unrar.dll.

Sample Image - unrar.png


In one of my latest projects, I was fiddling around with existing decompression classes, for Zip. I noticed though, that RAR had a far better compression ratio for certain files, and that the existing DLL handled everything itself, including the latest version of the RAR format.

How does the class use 'unrar.dll'?

Unrar.dll is freely distributable, and officially released by RarLabs. It is the interface which existing applications use to manipulate RAR archives. In the class, five functions are exported from the DLL and used.

HANDLE (WINAPI *OpenArchiveEx)(RAROpenArchiveDataEx *pArchiveData);
int    (WINAPI *CloseArchive)(HANDLE hArcData);
int    (WINAPI *ReadRARHeader)(HANDLE hArcData, 
               RARHeaderData *pHeaderData);
int    (WINAPI *ProcessRARFile)(HANDLE hArcData, 
               int iOperation, char* strDestFolder, char* strDestName);
int    (WINAPI *ReadRARHeaderEx)(HANDLE hArcData, 
               struct RARHeaderDataEx *HeaderData);

OpenArchiveEx is used in conjunction with the 'RAROpenArchiveDataEx' structure, to gain a handle to the open archive, if it succeeds. This can then be used in subsequent calls to the DLL. ReadRARHeader is what I use in the core UnRAR function. It is used to process the header of the RAR, to gain information about files contained in the RAR. It fills the RARHeaderData structure with information, including file name, packed size, unpacked etc. ProcessRARFile is used to perform an action on the current file inside the RAR archive. This can be to test, extract, or to skip the file altogether. After being called, it moves to the next file in the archive. CloseArchive is used to close the HANDLE gained from OpenArchiveEx. ReadRARHeaderEx is the same as ReadRARHeader, but contains extra information, such as a Unicode filename.

How it works

CUnrarDLL wraps the 'unrar.dll', freely available from the WinRAR website, and its interface, into a nice, easy-to-use MFC class. UnRARring archives couldn't be simpler.

Here's an example:

// Define an UnRAR object
CUnrarDLL unrarObj;
// Load the RAR file
// Set the output directory
// UnRAR the archive

This is all well and good, but what if you wanted to learn more about what's inside an archive, before you extract it? No problem. There are two ways to gain information. The first is done automatically when you call 'OpenRARFile'. Upon opening, CUnrarDLL loads all the files, their packed and unpacked sizes into a vector, which can be examined as below:

// Open the RAR file unrarObj.OpenRARFile("MyContent.rar");
// String to show
CString fileInfo;
// File
CUnrarFile curFile;
// Loop through the files and show them
for (int i = 0; i < unrarObj.GetNumberOfFiles(); i++) {
    curFile = unrarObj.GetFileAt(i);
    fileInfo.Format("File name: %s\nPacked size:" 
           " %I64d\nUnpacked size: %I64d",
           curFile.fileName, curFile.packSize, curFile.unpackSize);

Another method that has been implemented is 'ListFileNames', which will list only filenames into a CStringArray.


  • August 4th, 2005: Updated the article.
  • August 3rd, 2005: Initial release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

United States United States
No Biography provided

Comments and Discussions

Questionhow can get the correct filename when it's pathname more than 260characters? Pin
bobcod30-Jul-10 15:41
Memberbobcod30-Jul-10 15:41 
QuestionHelp me Pin
hoogenband16-Jul-08 4:55
Memberhoogenband16-Jul-08 4:55 
AnswerRe: Help me Pin
hoogenband17-Jul-08 21:05
Memberhoogenband17-Jul-08 21:05 
GeneralRe: Help me Pin
hoogenband17-Jul-08 22:33
Memberhoogenband17-Jul-08 22:33 
Questionprogress Pin
nonfilter8-Aug-06 20:04
Membernonfilter8-Aug-06 20:04 
Generalyour code is old! Pin
Ahfu29-Dec-05 21:39
MemberAhfu29-Dec-05 21:39 
GeneralRe: your code is old! Pin
.rich.w29-Dec-05 23:25 23:25 
GeneralRe: your code is old! Pin
Ahfu1-Jan-06 4:10
MemberAhfu1-Jan-06 4:10 
GeneralRe: your code is old! Pin
careprad4-Jul-07 17:10
Membercareprad4-Jul-07 17:10 
QuestionPasswords? Pin
Anonymous17-Aug-05 10:38
MemberAnonymous17-Aug-05 10:38 
AnswerRe: Passwords? Pin
.rich.w19-Aug-05 19:56 19:56 
GeneralInteresting article. Pin
WREY4-Aug-05 6:27
MemberWREY4-Aug-05 6:27 
GeneralRe: Interesting article. Pin
.rich.w4-Aug-05 6:43 6:43 
GeneralRe: Interesting article. Pin
.rich.w4-Aug-05 6:58 6:58 
GeneralRe: Interesting article. [modified] Pin
jzcdj29-Apr-08 22:00
Memberjzcdj29-Apr-08 22:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.