An MFC class to unpack Rar files, with multithread and GUI support
An MFC class to unpack Ear files, with multithread and GUI support.
Introduction
I have been looking for a way to extract Rar files automatically, in a similar way as is done by WinRar. The article CUnrarDLL 1.0 by rich.w. was cool; however, it does not meet all of my expectations.
In this article, I have offered an MFC class to unpack Rar files using unrar.dll (from RARLIB).
Some of its features include:
- It is an MFC based class and very handy to use.
For instance, the following snippet performs the simplest file extraction task:
CUnrarObject m_UnrarObject; // Declare an object instance
m_UnrarObject.SetInputPath("C:\\Test.rar"); // Input the file to be extracted
m_UnrarObject.SetOutputPath("D:\\Output"); // Set the output folder path
m_UnrarObject.Unpack(); // Do the actual extraction
You can extract A.rar, A.r00, A.r01,...
At the same time the files are being unpacked, you can display the file extraction results in a CListBox
, CStatic, or other GUI controls. This might be useful when you are dealing with a bulky archive, say a 10M to 20M file, or if the archive contains, say, 10,000 files. It notifies the users that it is still in process and alive...
This allows you to carry out other tasks simultaneously when the files are extracted, and your interface can still respond well.
After successful file unpacking, you might want to delete the original input archives, as is usually done manually. This class allows you to automatically delete them (e.g., A.rar, A.r00, A.r01,...) on task accomplishment.
Sample Usage
The class is expected to be simple in use, for instance:
CUnrarObject m_Obj;
m_Obj.SetInputPath("... ");
m_Obj.SetOutputPath("...");
BOOL CXXXDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//Hook the GUI control with the object
m_Obj.SetListBox(&m_ListBox);
m_Obj.SetStaticControl(&m_Static);
//Delete the original archives on finish.
m_Obj.SetCleanArchiveOption(TRUE);
}
//Use multithread functions to unpack the archive
m_Obj.Unpack(TRUE);
That is it.
Key Functions
void SetInputPath(CString InputPath)
: This function inputs the *.rar file to be unpacked.void SetOutputPath(CString OutputPath)
: This function specifies where to unpack the input *.rar files.CString GetInputPath()
: Returns the input archive to be unpacked.CString GetOutputPath()
: Returns the output folder where to extract the archives.void SetCleanArchiveOption(bool bRemove);
: Sets the option if the input archives will be automatically deleted on task finish. For instance, the input files test.rar, test.r00, test.r01 ... etc. will be deleted ifbRemove
isTRUE
.void SetListBox(CListBox *pBox)
: This function hooks the input listbox with the file unpacking operations; the current file unpacking results are displayed to the listbox so that the users can monitor the dynamic process.void SetStaticControl(CStatic *pStatic)
: Similar toSetListBox()
, this function hooks theCStatic
control with the file unpacking operations; the current file unpacking results are displayed to the static control so that the users can monitor the dynamic process.void Unpack(bool bMultiThread=TRUE)
: This function executes the file unpacking operation. IfbMultiThread
isTRUE
, then a new thread will be created to extract the archives, else the main thread in the current process will be used, i.e., sequential executions.
Acknowledgements
- I would like to thank RARLAB for providing such a neat and handy API interface.
- In the demo program, the article "How to Browse for a Folder" by Nitron was adapted in an MFC flavor, I hereby thank him for his efforts.
- I would also like to draw your attention to the fact that one of my freewares, FlashUnpack, is based on the above MFC class. You can download it at http://flashunpack.3322.org/. I welcome your feedback.
History
- Jul 27, 2006, minor help update.
- Jul 26, 2006, initial release to the public.