UnrarObject.cpp
上传用户:sz4588
上传日期:2022-05-26
资源大小:2253k
文件大小:7k
源码类别:

压缩解压

开发平台:

Visual C++

  1. // UnrarObject.cpp: implementation of the CUnrarObject class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "UnrarObject.h"
  6. #include "gAPI.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. //------------------------------------------------------------------------
  16. CUnrarObject::CUnrarObject()
  17. {
  18. m_hArchive=NULL;
  19. bCleanOriginalArchive=FALSE;
  20. ResetGuiControls();
  21. }
  22. //------------------------------------------------------------------------
  23. CUnrarObject::~CUnrarObject()
  24. {
  25. if(pGUIWindows) delete pGUIWindows;
  26. if(m_hArchive)
  27. {
  28. RARCloseArchive(m_hArchive);
  29. m_hArchive=NULL;
  30. }
  31. }
  32. //------------------------------------------------------------------------
  33. //------------------------------------------------------------------------
  34. int CUnrarObject::SetInputPath(CString InputPath)
  35. {
  36. m_InputPath=InputPath;
  37. //[1] Get the handle 
  38. char CmtBuf[16384]; //Comment buffer
  39. RAROpenArchiveDataEx ArchiveData;
  40. memset(&ArchiveData,0,sizeof(ArchiveData));
  41. ArchiveData.ArcName=InputPath.GetBuffer(1024);
  42. ArchiveData.CmtBuf=CmtBuf;
  43. ArchiveData.CmtBufSize=sizeof(CmtBuf);
  44. ArchiveData.OpenMode=RAR_OM_EXTRACT;
  45. m_hArchive=RAROpenArchiveEx(&ArchiveData);
  46. if(m_hArchive==NULL)
  47. {
  48. printf("Error open packed files %s",InputPath);
  49. return 0;
  50. }
  51. //[2] hook the callback functions for GUI display
  52. RARSetCallback(m_hArchive,ExtractCallbackProc,(LONG)this);
  53. return 1;
  54. }
  55. void CUnrarObject::SetCleanArchiveOption(bool bRemove)
  56. {
  57. bCleanOriginalArchive=bRemove;
  58. }
  59. //------------------------------------------------------------------------
  60. //////////////////////////////////////////////////////////////////////
  61. // Unpacking operations
  62. //////////////////////////////////////////////////////////////////////
  63. //------------------------------------------------------------------------
  64. //The multi-thread function
  65. UINT CUnrarObject::UnpackThreadFun(LPVOID pParam)
  66. {
  67. CUnrarObject* pThis=(CUnrarObject*)pParam;
  68. if(pThis==NULL)
  69. return -1;
  70. if(pThis->m_hArchive)
  71. {
  72. pThis->Update_Gui_Info(_T("-Unpacking ")+ApiGetFileName(pThis->m_InputPath));
  73. pThis->UnpackedArchives.Add(pThis->m_InputPath); //For the final deletion/cleaning 
  74. RARHeaderData HeaderData;
  75. int ReadHeaderCode;
  76. ///////////////////////////////////////////////////////////////////
  77. //For each file in the rar 
  78. while ((ReadHeaderCode=RARReadHeader(pThis->m_hArchive,&HeaderData)==0))
  79. {
  80. CString Info;
  81. Info.Format("        ==> %s Extracted",HeaderData.FileName);
  82. pThis->Update_Gui_Info(Info);
  83. int nReturnCode=RARProcessFile( pThis->m_hArchive,RAR_EXTRACT,
  84. pThis->m_OutputPath.GetBuffer(1024),NULL);
  85. switch(nReturnCode)
  86. {
  87. case 0: break; //Success
  88. case ERAR_BAD_DATA:         TRACE("File Extraction: Bad data, possibly CRC error"); return FALSE;
  89. case ERAR_BAD_ARCHIVE:      TRACE("File Extraction: Volume is not valid RAR archive"); return FALSE;
  90. case ERAR_UNKNOWN_FORMAT:   TRACE("File Extraction: Unknown archive format"); return FALSE;
  91. case ERAR_EOPEN:            TRACE("File Extraction: Volume open error"); return FALSE;
  92. case ERAR_ECREATE:          TRACE("File Extraction: File create error"); return FALSE;
  93. case ERAR_ECLOSE:           TRACE("File Extraction: File close error"); return FALSE;
  94. case ERAR_EREAD:            TRACE("File Extraction: Read error"); return FALSE;
  95. case ERAR_EWRITE:           TRACE("File Extraction: Write error"); return FALSE;
  96. }
  97. }
  98. ///////////////////////////////////////////////////////////////////
  99. }
  100. pThis->Update_Gui_Info("Success!");
  101. pThis->CleanOriginalArchive(pThis->bCleanOriginalArchive);
  102. return 0;
  103. }
  104. //------------------------------------------------------------------------
  105. //------------------------------------------------------------------------
  106. //Use this to call the unpack function
  107. void CUnrarObject::Unpack(bool bMultiThread)
  108. {
  109. if(SetInputPath(m_InputPath)==0)return;
  110. if(m_hArchive==NULL)return;
  111. if(bMultiThread)
  112. AfxBeginThread(UnpackThreadFun,this);
  113. else
  114.      UnpackThreadFun((LPVOID)this);
  115. }
  116. //------------------------------------------------------------------------
  117. //The callback function, following the signature of RARLAB API
  118. int CUnrarObject::ExtractCallbackProc(UINT msg, LONG UserData, LONG P1, LONG P2)
  119. {
  120. CUnrarObject* pThis=(CUnrarObject*)UserData;
  121. switch(msg)
  122. {
  123.     case UCM_CHANGEVOLUME:
  124. if (P2==RAR_VOL_ASK)
  125. {
  126. CString Info; 
  127. Info.Format("A volume %s is required, terminate now",ApiGetFileName((char *)P1));
  128. TRACE(Info);
  129. exit(-1); 
  130. }
  131. if (P2==RAR_VOL_NOTIFY)
  132. {
  133. CString Path=(char *)P1;
  134. pThis->UnpackedArchives.Add(Path); //For file deletions;
  135. pThis->Update_Gui_Info(_T("-Unpacking ")+ApiGetFileName(Path));
  136. }
  137.     case UCM_PROCESSDATA: { }
  138.     case UCM_NEEDPASSWORD:{ }
  139. }
  140. return(0);
  141. }
  142. //------------------------------------------------------------------------
  143. void CUnrarObject::CleanOriginalArchive(BOOL bClearFilesOnSuccess)
  144. {
  145. if(bClearFilesOnSuccess)
  146. {
  147. RARCloseArchive(m_hArchive);
  148. m_hArchive=NULL;
  149. for(int i=0;i<UnpackedArchives.GetSize();i++)
  150. DeleteFile(UnpackedArchives[i]); //Direct delete, not RecycleBin!
  151. }
  152. }
  153. //------------------------------------------------------------------------
  154. //////////////////////////////////////////////////////////////////////
  155. // GUI Controls 
  156. //////////////////////////////////////////////////////////////////////
  157. //------------------------------------------------------------------------
  158. void CUnrarObject::ResetGuiControls()
  159. {
  160. pGUIWindows=new CGuiStruct;
  161. pGUIWindows->m_pListBox=NULL;
  162. pGUIWindows->m_pstatic=NULL;
  163. }
  164. void CUnrarObject::Update_Gui_Info(CString Info,  BOOL bClearDataFirst)
  165. {
  166. if(Info.IsEmpty())
  167. return;
  168. if(pGUIWindows->m_pListBox)
  169. {
  170. if(bClearDataFirst)
  171. pGUIWindows->m_pListBox->ResetContent();
  172. int n=pGUIWindows->m_pListBox->GetCount();
  173. pGUIWindows->m_pListBox->InsertString(n,Info);
  174. pGUIWindows->m_pListBox->SetCurSel(n);
  175. pGUIWindows->m_pListBox->Invalidate();
  176. }
  177. if(pGUIWindows->m_pstatic)
  178. {
  179. pGUIWindows->m_pstatic->SetWindowText(Info);
  180. pGUIWindows->m_pstatic->Invalidate();
  181. }
  182. //Append your own GUI control update code here
  183. }
  184. //------------------------------------------------------------------------