Click here to Skip to main content
6,293,171 members and growing! (11,729 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » File Formats     Intermediate

CUnrarDLL 1.0 - An MFC UnRAR class

By .rich.w

An easy to use class based around unrar.dll.
VC6, Windows, MFC, Dev
Posted:3 Aug 2005
Updated:9 Aug 2005
Views:39,731
Bookmarked:25 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
12 votes for this article.
Popularity: 4.38 Rating: 4.06 out of 5
1 vote, 8.3%
1
2 votes, 16.7%
2

3
2 votes, 16.7%
4
7 votes, 58.3%
5

Sample Image - unrar.png

Introduction

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

unrarObj.OpenRARFile("MyContent.rar");
// Set the output directory

unrarObj.SetOutputDirectory("C:\\Output");
// UnRAR the archive

unrarObj.UnRARArchive();

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);
    MessageBox(fileInfo);
}

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

Updates

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

License

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

.rich.w


Member
I'm a random 18 year old guy from England.. In my free time i'm usually skateboarding or hanging around places being a nuisance.

I started programming with MFC about a year ago after fiddling around with mIRCScript for 2 years (ugh).
Location: United States United States

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
QuestionHelp me Pinmemberhoogenband5:55 16 Jul '08  
AnswerRe: Help me Pinmemberhoogenband22:05 17 Jul '08  
GeneralRe: Help me Pinmemberhoogenband23:33 17 Jul '08  
Questionprogress Pinmembernonfilter21:04 8 Aug '06  
Generalyour code is old! PinmemberAhfu22:39 29 Dec '05  
GeneralRe: your code is old! Pinmember.rich.w0:25 30 Dec '05  
GeneralRe: your code is old! PinmemberAhfu5:10 1 Jan '06  
GeneralRe: your code is old! Pinmembercareprad18:10 4 Jul '07  
GeneralPasswords? PinsussAnonymous11:38 17 Aug '05  
GeneralRe: Passwords? Pinmember.rich.w20:56 19 Aug '05  
GeneralInteresting article. PinmemberWREY7:27 4 Aug '05  
GeneralRe: Interesting article. Pinmember.rich.w7:43 4 Aug '05  
GeneralRe: Interesting article. Pinmember.rich.w7:58 4 Aug '05  
GeneralRe: Interesting article. [modified] Pinmemberjzcdj23:00 29 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Aug 2005
Editor: Smitha Vijayan
Copyright 2005 by .rich.w
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project