Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

An MFC class to unpack Rar files, with multithread and GUI support

2.51/5 (13 votes)
25 Jul 2006CPOL3 min read 1   2K  
An MFC class to unpack Ear files, with multithread and GUI support.

Sample image

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:

    C++
    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
  • It supports multiple volume Rar files.
  • You can extract A.rar, A.r00, A.r01,...

  • Support GUI update to reflect the unpack process.
  • 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...

  • Support multithread file unpacking.
  • This allows you to carry out other tasks simultaneously when the files are extracted, and your interface can still respond well.

  • Support automatic archive cleaning.
  • 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:

C++
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 if bRemove is TRUE.
  • 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 to SetListBox(), this function hooks the CStatic 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. If bMultiThread is TRUE, 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)