ArchiveExtractCallback.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // ArchiveExtractCallback.cpp
  2. #include "StdAfx.h"
  3. #include "ArchiveExtractCallback.h"
  4. #include "Common/Wildcard.h"
  5. #include "Common/StringConvert.h"
  6. #include "Common/ComTry.h"
  7. #include "Windows/FileDir.h"
  8. #include "Windows/FileFind.h"
  9. #include "Windows/Time.h"
  10. #include "Windows/Defs.h"
  11. #include "Windows/PropVariant.h"
  12. #include "Windows/PropVariantConversions.h"
  13. #include "../../Common/FilePathAutoRename.h"
  14. #include "../Common/ExtractingFilePath.h"
  15. #include "OpenArchive.h"
  16. using namespace NWindows;
  17. static const wchar_t *kCantAutoRename = L"ERROR: Can not create file with auto name";
  18. static const wchar_t *kCantRenameFile = L"ERROR: Can not rename existing file ";
  19. static const wchar_t *kCantDeleteOutputFile = L"ERROR: Can not delete output file ";
  20. void CArchiveExtractCallback::Init(
  21. IInArchive *archiveHandler,
  22. IFolderArchiveExtractCallback *extractCallback2,
  23. bool stdOutMode,
  24. const UString &directoryPath,
  25. const UStringVector &removePathParts,
  26. const UString &itemDefaultName,
  27. const FILETIME &utcLastWriteTimeDefault,
  28. UInt32 attributesDefault,
  29. UInt64 packSize)
  30. {
  31. _stdOutMode = stdOutMode;
  32. _numErrors = 0;
  33. _unpTotal = 1;
  34. _packTotal = packSize;
  35. _extractCallback2 = extractCallback2;
  36. _compressProgress.Release();
  37. _extractCallback2.QueryInterface(IID_ICompressProgressInfo, &_compressProgress);
  38. LocalProgressSpec->Init(extractCallback2, true);
  39. LocalProgressSpec->SendProgress = false;
  40. _itemDefaultName = itemDefaultName;
  41. _utcLastWriteTimeDefault = utcLastWriteTimeDefault;
  42. _attributesDefault = attributesDefault;
  43. _removePathParts = removePathParts;
  44. _archiveHandler = archiveHandler;
  45. _directoryPath = directoryPath;
  46. NFile::NName::NormalizeDirPathPrefix(_directoryPath);
  47. }
  48. STDMETHODIMP CArchiveExtractCallback::SetTotal(UInt64 size)
  49. {
  50. COM_TRY_BEGIN
  51. _unpTotal = size;
  52. if (!_multiArchives && _extractCallback2)
  53. return _extractCallback2->SetTotal(size);
  54. return S_OK;
  55. COM_TRY_END
  56. }
  57. static void NormalizeVals(UInt64 &v1, UInt64 &v2)
  58. {
  59. const UInt64 kMax = (UInt64)1 << 31;
  60. while (v1 > kMax)
  61. {
  62. v1 >>= 1;
  63. v2 >>= 1;
  64. }
  65. }
  66. static UInt64 MyMultDiv64(UInt64 unpCur, UInt64 unpTotal, UInt64 packTotal)
  67. {
  68. NormalizeVals(packTotal, unpTotal);
  69. NormalizeVals(unpCur, unpTotal);
  70. if (unpTotal == 0)
  71. unpTotal = 1;
  72. return unpCur * packTotal / unpTotal;
  73. }
  74. STDMETHODIMP CArchiveExtractCallback::SetCompleted(const UInt64 *completeValue)
  75. {
  76. COM_TRY_BEGIN
  77. if (!_extractCallback2)
  78. return S_OK;
  79. if (_multiArchives)
  80. {
  81. if (completeValue != NULL)
  82. {
  83. UInt64 packCur = LocalProgressSpec->InSize + MyMultDiv64(*completeValue, _unpTotal, _packTotal);
  84. return _extractCallback2->SetCompleted(&packCur);
  85. }
  86. }
  87. return _extractCallback2->SetCompleted(completeValue);
  88. COM_TRY_END
  89. }
  90. STDMETHODIMP CArchiveExtractCallback::SetRatioInfo(const UInt64 *inSize, const UInt64 *outSize)
  91. {
  92. COM_TRY_BEGIN
  93. return _localProgress->SetRatioInfo(inSize, outSize);
  94. COM_TRY_END
  95. }
  96. void CArchiveExtractCallback::CreateComplexDirectory(const UStringVector &dirPathParts, UString &fullPath)
  97. {
  98. fullPath = _directoryPath;
  99. for(int i = 0; i < dirPathParts.Size(); i++)
  100. {
  101. if (i > 0)
  102. fullPath += wchar_t(NFile::NName::kDirDelimiter);
  103. fullPath += dirPathParts[i];
  104. NFile::NDirectory::MyCreateDirectory(fullPath);
  105. }
  106. }
  107. static UString MakePathNameFromParts(const UStringVector &parts)
  108. {
  109. UString result;
  110. for(int i = 0; i < parts.Size(); i++)
  111. {
  112. if(i != 0)
  113. result += wchar_t(NFile::NName::kDirDelimiter);
  114. result += parts[i];
  115. }
  116. return result;
  117. }
  118. HRESULT CArchiveExtractCallback::GetTime(int index, PROPID propID, FILETIME &filetime, bool &filetimeIsDefined)
  119. {
  120. filetimeIsDefined = false;
  121. NCOM::CPropVariant prop;
  122. RINOK(_archiveHandler->GetProperty(index, propID, &prop));
  123. if (prop.vt == VT_FILETIME)
  124. {
  125. filetime = prop.filetime;
  126. filetimeIsDefined = (filetime.dwHighDateTime != 0 || filetime.dwLowDateTime != 0);
  127. }
  128. else if (prop.vt != VT_EMPTY)
  129. return E_FAIL;
  130. return S_OK;
  131. }
  132. STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index, ISequentialOutStream **outStream, Int32 askExtractMode)
  133. {
  134. COM_TRY_BEGIN
  135. *outStream = 0;
  136. _outFileStream.Release();
  137. _encrypted = false;
  138. _isSplit = false;
  139. _curSize = 0;
  140. UString fullPath;
  141. RINOK(GetArchiveItemPath(_archiveHandler, index, _itemDefaultName, fullPath));
  142. RINOK(IsArchiveItemFolder(_archiveHandler, index, _processedFileInfo.IsDirectory));
  143. _filePath = fullPath;
  144. {
  145. NCOM::CPropVariant prop;
  146. RINOK(_archiveHandler->GetProperty(index, kpidPosition, &prop));
  147. if (prop.vt != VT_EMPTY)
  148. {
  149. if (prop.vt != VT_UI8)
  150. return E_FAIL;
  151. _position = prop.uhVal.QuadPart;
  152. _isSplit = true;
  153. }
  154. }
  155. RINOK(IsArchiveItemProp(_archiveHandler, index, kpidEncrypted, _encrypted));
  156. bool newFileSizeDefined;
  157. UInt64 newFileSize;
  158. {
  159. NCOM::CPropVariant prop;
  160. RINOK(_archiveHandler->GetProperty(index, kpidSize, &prop));
  161. newFileSizeDefined = (prop.vt != VT_EMPTY);
  162. if (newFileSizeDefined)
  163. {
  164. newFileSize = ConvertPropVariantToUInt64(prop);
  165. _curSize = newFileSize;
  166. }
  167. }
  168. if(askExtractMode == NArchive::NExtract::NAskMode::kExtract)
  169. {
  170. if (_stdOutMode)
  171. {
  172. CMyComPtr<ISequentialOutStream> outStreamLoc = new CStdOutFileStream;
  173. *outStream = outStreamLoc.Detach();
  174. return S_OK;
  175. }
  176. {
  177. NCOM::CPropVariant prop;
  178. RINOK(_archiveHandler->GetProperty(index, kpidAttributes, &prop));
  179. if (prop.vt == VT_EMPTY)
  180. {
  181. _processedFileInfo.Attributes = _attributesDefault;
  182. _processedFileInfo.AttributesAreDefined = false;
  183. }
  184. else
  185. {
  186. if (prop.vt != VT_UI4)
  187. return E_FAIL;
  188. _processedFileInfo.Attributes = prop.ulVal;
  189. _processedFileInfo.AttributesAreDefined = true;
  190. }
  191. }
  192. RINOK(GetTime(index, kpidCreationTime, _processedFileInfo.CreationTime,
  193. _processedFileInfo.IsCreationTimeDefined));
  194. RINOK(GetTime(index, kpidLastWriteTime, _processedFileInfo.LastWriteTime,
  195. _processedFileInfo.IsLastWriteTimeDefined));
  196. RINOK(GetTime(index, kpidLastAccessTime, _processedFileInfo.LastAccessTime,
  197. _processedFileInfo.IsLastAccessTimeDefined));
  198. bool isAnti = false;
  199. RINOK(IsArchiveItemProp(_archiveHandler, index, kpidIsAnti, isAnti));
  200. UStringVector pathParts;
  201. SplitPathToParts(fullPath, pathParts);
  202. if(pathParts.IsEmpty())
  203. return E_FAIL;
  204. int numRemovePathParts = 0;
  205. switch(_pathMode)
  206. {
  207. case NExtract::NPathMode::kFullPathnames:
  208. break;
  209. case NExtract::NPathMode::kCurrentPathnames:
  210. {
  211. numRemovePathParts = _removePathParts.Size();
  212. if (pathParts.Size() <= numRemovePathParts)
  213. return E_FAIL;
  214. for (int i = 0; i < numRemovePathParts; i++)
  215. if (_removePathParts[i].CompareNoCase(pathParts[i]) != 0)
  216. return E_FAIL;
  217. break;
  218. }
  219. case NExtract::NPathMode::kNoPathnames:
  220. {
  221. numRemovePathParts = pathParts.Size() - 1;
  222. break;
  223. }
  224. }
  225. pathParts.Delete(0, numRemovePathParts);
  226. MakeCorrectPath(pathParts);
  227. UString processedPath = MakePathNameFromParts(pathParts);
  228. if (!isAnti)
  229. {
  230. if (!_processedFileInfo.IsDirectory)
  231. {
  232. if (!pathParts.IsEmpty())
  233. pathParts.DeleteBack();
  234. }
  235. if (!pathParts.IsEmpty())
  236. {
  237. UString fullPathNew;
  238. CreateComplexDirectory(pathParts, fullPathNew);
  239. if (_processedFileInfo.IsDirectory)
  240. NFile::NDirectory::SetDirTime(fullPathNew,
  241. (WriteCreated && _processedFileInfo.IsCreationTimeDefined) ? &_processedFileInfo.CreationTime : NULL,
  242. (WriteAccessed && _processedFileInfo.IsLastAccessTimeDefined) ? &_processedFileInfo.LastAccessTime : NULL,
  243. (WriteModified && _processedFileInfo.IsLastWriteTimeDefined) ? &_processedFileInfo.LastWriteTime : &_utcLastWriteTimeDefault);
  244. }
  245. }
  246. UString fullProcessedPath = _directoryPath + processedPath;
  247. if(_processedFileInfo.IsDirectory)
  248. {
  249. _diskFilePath = fullProcessedPath;
  250. if (isAnti)
  251. NFile::NDirectory::MyRemoveDirectory(_diskFilePath);
  252. return S_OK;
  253. }
  254. if (!_isSplit)
  255. {
  256. NFile::NFind::CFileInfoW fileInfo;
  257. if(NFile::NFind::FindFile(fullProcessedPath, fileInfo))
  258. {
  259. switch(_overwriteMode)
  260. {
  261. case NExtract::NOverwriteMode::kSkipExisting:
  262. return S_OK;
  263. case NExtract::NOverwriteMode::kAskBefore:
  264. {
  265. Int32 overwiteResult;
  266. RINOK(_extractCallback2->AskOverwrite(
  267. fullProcessedPath, &fileInfo.LastWriteTime, &fileInfo.Size, fullPath,
  268. _processedFileInfo.IsLastWriteTimeDefined ? &_processedFileInfo.LastWriteTime : NULL,
  269. newFileSizeDefined ? &newFileSize : NULL,
  270. &overwiteResult))
  271. switch(overwiteResult)
  272. {
  273. case NOverwriteAnswer::kCancel:
  274. return E_ABORT;
  275. case NOverwriteAnswer::kNo:
  276. return S_OK;
  277. case NOverwriteAnswer::kNoToAll:
  278. _overwriteMode = NExtract::NOverwriteMode::kSkipExisting;
  279. return S_OK;
  280. case NOverwriteAnswer::kYesToAll:
  281. _overwriteMode = NExtract::NOverwriteMode::kWithoutPrompt;
  282. break;
  283. case NOverwriteAnswer::kYes:
  284. break;
  285. case NOverwriteAnswer::kAutoRename:
  286. _overwriteMode = NExtract::NOverwriteMode::kAutoRename;
  287. break;
  288. default:
  289. return E_FAIL;
  290. }
  291. }
  292. }
  293. if (_overwriteMode == NExtract::NOverwriteMode::kAutoRename)
  294. {
  295. if (!AutoRenamePath(fullProcessedPath))
  296. {
  297. UString message = UString(kCantAutoRename) + fullProcessedPath;
  298. RINOK(_extractCallback2->MessageError(message));
  299. return E_FAIL;
  300. }
  301. }
  302. else if (_overwriteMode == NExtract::NOverwriteMode::kAutoRenameExisting)
  303. {
  304. UString existPath = fullProcessedPath;
  305. if (!AutoRenamePath(existPath))
  306. {
  307. UString message = kCantAutoRename + fullProcessedPath;
  308. RINOK(_extractCallback2->MessageError(message));
  309. return E_FAIL;
  310. }
  311. if(!NFile::NDirectory::MyMoveFile(fullProcessedPath, existPath))
  312. {
  313. UString message = UString(kCantRenameFile) + fullProcessedPath;
  314. RINOK(_extractCallback2->MessageError(message));
  315. return E_FAIL;
  316. }
  317. }
  318. else
  319. if (!NFile::NDirectory::DeleteFileAlways(fullProcessedPath))
  320. {
  321. UString message = UString(kCantDeleteOutputFile) + fullProcessedPath;
  322. RINOK(_extractCallback2->MessageError(message));
  323. return S_OK;
  324. // return E_FAIL;
  325. }
  326. }
  327. }
  328. if (!isAnti)
  329. {
  330. _outFileStreamSpec = new COutFileStream;
  331. CMyComPtr<ISequentialOutStream> outStreamLoc(_outFileStreamSpec);
  332. if (!_outFileStreamSpec->Open(fullProcessedPath, _isSplit ? OPEN_ALWAYS: CREATE_ALWAYS))
  333. {
  334. // if (::GetLastError() != ERROR_FILE_EXISTS || !isSplit)
  335. {
  336. UString message = L"can not open output file " + fullProcessedPath;
  337. RINOK(_extractCallback2->MessageError(message));
  338. return S_OK;
  339. }
  340. }
  341. if (_isSplit)
  342. {
  343. RINOK(_outFileStreamSpec->Seek(_position, STREAM_SEEK_SET, NULL));
  344. }
  345. _outFileStream = outStreamLoc;
  346. *outStream = outStreamLoc.Detach();
  347. }
  348. _diskFilePath = fullProcessedPath;
  349. }
  350. else
  351. {
  352. *outStream = NULL;
  353. }
  354. return S_OK;
  355. COM_TRY_END
  356. }
  357. STDMETHODIMP CArchiveExtractCallback::PrepareOperation(Int32 askExtractMode)
  358. {
  359. COM_TRY_BEGIN
  360. _extractMode = false;
  361. switch (askExtractMode)
  362. {
  363. case NArchive::NExtract::NAskMode::kExtract:
  364. _extractMode = true;
  365. };
  366. return _extractCallback2->PrepareOperation(_filePath, _processedFileInfo.IsDirectory,
  367. askExtractMode, _isSplit ? &_position: 0);
  368. COM_TRY_END
  369. }
  370. STDMETHODIMP CArchiveExtractCallback::SetOperationResult(Int32 operationResult)
  371. {
  372. COM_TRY_BEGIN
  373. switch(operationResult)
  374. {
  375. case NArchive::NExtract::NOperationResult::kOK:
  376. case NArchive::NExtract::NOperationResult::kUnSupportedMethod:
  377. case NArchive::NExtract::NOperationResult::kCRCError:
  378. case NArchive::NExtract::NOperationResult::kDataError:
  379. break;
  380. default:
  381. _outFileStream.Release();
  382. return E_FAIL;
  383. }
  384. if (_outFileStream != NULL)
  385. {
  386. _outFileStreamSpec->SetTime(
  387. (WriteCreated && _processedFileInfo.IsCreationTimeDefined) ? &_processedFileInfo.CreationTime : NULL,
  388. (WriteAccessed && _processedFileInfo.IsLastAccessTimeDefined) ? &_processedFileInfo.LastAccessTime : NULL,
  389. (WriteModified && _processedFileInfo.IsLastWriteTimeDefined) ? &_processedFileInfo.LastWriteTime : &_utcLastWriteTimeDefault);
  390. _curSize = _outFileStreamSpec->ProcessedSize;
  391. RINOK(_outFileStreamSpec->Close());
  392. _outFileStream.Release();
  393. }
  394. UnpackSize += _curSize;
  395. if (_processedFileInfo.IsDirectory)
  396. NumFolders++;
  397. else
  398. NumFiles++;
  399. if (_extractMode && _processedFileInfo.AttributesAreDefined)
  400. NFile::NDirectory::MySetFileAttributes(_diskFilePath, _processedFileInfo.Attributes);
  401. RINOK(_extractCallback2->SetOperationResult(operationResult, _encrypted));
  402. return S_OK;
  403. COM_TRY_END
  404. }
  405. /*
  406. STDMETHODIMP CArchiveExtractCallback::GetInStream(
  407. const wchar_t *name, ISequentialInStream **inStream)
  408. {
  409. COM_TRY_BEGIN
  410. CInFileStream *inFile = new CInFileStream;
  411. CMyComPtr<ISequentialInStream> inStreamTemp = inFile;
  412. if (!inFile->Open(_srcDirectoryPrefix + name))
  413. return ::GetLastError();
  414. *inStream = inStreamTemp.Detach();
  415. return S_OK;
  416. COM_TRY_END
  417. }
  418. */
  419. STDMETHODIMP CArchiveExtractCallback::CryptoGetTextPassword(BSTR *password)
  420. {
  421. COM_TRY_BEGIN
  422. if (!_cryptoGetTextPassword)
  423. {
  424. RINOK(_extractCallback2.QueryInterface(IID_ICryptoGetTextPassword,
  425. &_cryptoGetTextPassword));
  426. }
  427. return _cryptoGetTextPassword->CryptoGetTextPassword(password);
  428. COM_TRY_END
  429. }