Client7z.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. // Client7z.cpp
  2. #include "StdAfx.h"
  3. #include "Common/MyInitGuid.h"
  4. #include "Common/StringConvert.h"
  5. #include "Common/IntToString.h"
  6. #include "Windows/PropVariant.h"
  7. #include "Windows/PropVariantConversions.h"
  8. #include "Windows/DLL.h"
  9. #include "Windows/FileDir.h"
  10. #include "Windows/FileName.h"
  11. #include "Windows/FileFind.h"
  12. #include "../../Common/FileStreams.h"
  13. #include "../../Archive/IArchive.h"
  14. #include "../../IPassword.h"
  15. #include "../../MyVersion.h"
  16. // {23170F69-40C1-278A-1000-000110070000}
  17. DEFINE_GUID(CLSID_CFormat7z,
  18. 0x23170F69, 0x40C1, 0x278A, 0x10, 0x00, 0x00, 0x01, 0x10, 0x07, 0x00, 0x00);
  19. using namespace NWindows;
  20. #define kDllName "7z.dll"
  21. static const char *kCopyrightString = MY_7ZIP_VERSION
  22. " (" kDllName " client) "
  23. MY_COPYRIGHT " " MY_DATE;
  24. static const char *kHelpString =
  25. "Usage: Client7z.exe [a | l | x ] archive.7z [fileName ...]\n"
  26. "Examples:\n"
  27. " Client7z.exe a archive.7z f1.txt f2.txt : compress two files to archive.7z\n"
  28. " Client7z.exe l archive.7z : List contents of archive.7z\n"
  29. " Client7z.exe x archive.7z : eXtract files from archive.7z\n";
  30. typedef UINT32 (WINAPI * CreateObjectFunc)(
  31. const GUID *clsID,
  32. const GUID *interfaceID,
  33. void **outObject);
  34. #ifdef _WIN32
  35. #ifndef _UNICODE
  36. bool g_IsNT = false;
  37. static inline bool IsItWindowsNT()
  38. {
  39. OSVERSIONINFO versionInfo;
  40. versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
  41. if (!::GetVersionEx(&versionInfo))
  42. return false;
  43. return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
  44. }
  45. #endif
  46. #endif
  47. void PrintString(const UString &s)
  48. {
  49. printf("%s", (LPCSTR)GetOemString(s));
  50. }
  51. void PrintString(const AString &s)
  52. {
  53. printf("%s", (LPCSTR)s);
  54. }
  55. void PrintNewLine()
  56. {
  57. PrintString("\n");
  58. }
  59. void PrintStringLn(const AString &s)
  60. {
  61. PrintString(s);
  62. PrintNewLine();
  63. }
  64. void PrintError(const AString &s)
  65. {
  66. PrintNewLine();
  67. PrintString(s);
  68. PrintNewLine();
  69. }
  70. static HRESULT IsArchiveItemProp(IInArchive *archive, UInt32 index, PROPID propID, bool &result)
  71. {
  72. NCOM::CPropVariant prop;
  73. RINOK(archive->GetProperty(index, propID, &prop));
  74. if(prop.vt == VT_BOOL)
  75. result = VARIANT_BOOLToBool(prop.boolVal);
  76. else if (prop.vt == VT_EMPTY)
  77. result = false;
  78. else
  79. return E_FAIL;
  80. return S_OK;
  81. }
  82. static HRESULT IsArchiveItemFolder(IInArchive *archive, UInt32 index, bool &result)
  83. {
  84. return IsArchiveItemProp(archive, index, kpidIsFolder, result);
  85. }
  86. static const wchar_t *kEmptyFileAlias = L"[Content]";
  87. //////////////////////////////////////////////////////////////
  88. // Archive Open callback class
  89. class CArchiveOpenCallback:
  90. public IArchiveOpenCallback,
  91. public ICryptoGetTextPassword,
  92. public CMyUnknownImp
  93. {
  94. public:
  95. MY_UNKNOWN_IMP1(ICryptoGetTextPassword)
  96. STDMETHOD(SetTotal)(const UInt64 *files, const UInt64 *bytes);
  97. STDMETHOD(SetCompleted)(const UInt64 *files, const UInt64 *bytes);
  98. STDMETHOD(CryptoGetTextPassword)(BSTR *password);
  99. bool PasswordIsDefined;
  100. UString Password;
  101. CArchiveOpenCallback() : PasswordIsDefined(false) {}
  102. };
  103. STDMETHODIMP CArchiveOpenCallback::SetTotal(const UInt64 * /* files */, const UInt64 * /* bytes */)
  104. {
  105. return S_OK;
  106. }
  107. STDMETHODIMP CArchiveOpenCallback::SetCompleted(const UInt64 * /* files */, const UInt64 * /* bytes */)
  108. {
  109. return S_OK;
  110. }
  111. STDMETHODIMP CArchiveOpenCallback::CryptoGetTextPassword(BSTR *password)
  112. {
  113. if (!PasswordIsDefined)
  114. {
  115. // You can ask real password here from user
  116. // Password = GetPassword(OutStream);
  117. // PasswordIsDefined = true;
  118. PrintError("Password is not defined");
  119. return E_ABORT;
  120. }
  121. CMyComBSTR tempName(Password);
  122. *password = tempName.Detach();
  123. return S_OK;
  124. }
  125. //////////////////////////////////////////////////////////////
  126. // Archive Extracting callback class
  127. static const wchar_t *kCantDeleteOutputFile = L"ERROR: Can not delete output file ";
  128. static const char *kTestingString = "Testing ";
  129. static const char *kExtractingString = "Extracting ";
  130. static const char *kSkippingString = "Skipping ";
  131. static const char *kUnsupportedMethod = "Unsupported Method";
  132. static const char *kCRCFailed = "CRC Failed";
  133. static const char *kDataError = "Data Error";
  134. static const char *kUnknownError = "Unknown Error";
  135. class CArchiveExtractCallback:
  136. public IArchiveExtractCallback,
  137. public ICryptoGetTextPassword,
  138. public CMyUnknownImp
  139. {
  140. public:
  141. MY_UNKNOWN_IMP1(ICryptoGetTextPassword)
  142. // IProgress
  143. STDMETHOD(SetTotal)(UInt64 size);
  144. STDMETHOD(SetCompleted)(const UInt64 *completeValue);
  145. // IArchiveExtractCallback
  146. STDMETHOD(GetStream)(UInt32 index, ISequentialOutStream **outStream, Int32 askExtractMode);
  147. STDMETHOD(PrepareOperation)(Int32 askExtractMode);
  148. STDMETHOD(SetOperationResult)(Int32 resultEOperationResult);
  149. // ICryptoGetTextPassword
  150. STDMETHOD(CryptoGetTextPassword)(BSTR *aPassword);
  151. private:
  152. CMyComPtr<IInArchive> _archiveHandler;
  153. UString _directoryPath; // Output directory
  154. UString _filePath; // name inside arcvhive
  155. UString _diskFilePath; // full path to file on disk
  156. bool _extractMode;
  157. struct CProcessedFileInfo
  158. {
  159. FILETIME UTCLastWriteTime;
  160. UInt32 Attributes;
  161. bool IsDirectory;
  162. bool AttributesAreDefined;
  163. bool UTCLastWriteTimeIsDefined;
  164. } _processedFileInfo;
  165. COutFileStream *_outFileStreamSpec;
  166. CMyComPtr<ISequentialOutStream> _outFileStream;
  167. public:
  168. void Init(IInArchive *archiveHandler, const UString &directoryPath);
  169. UInt64 NumErrors;
  170. bool PasswordIsDefined;
  171. UString Password;
  172. CArchiveExtractCallback() : PasswordIsDefined(false) {}
  173. };
  174. void CArchiveExtractCallback::Init(IInArchive *archiveHandler, const UString &directoryPath)
  175. {
  176. NumErrors = 0;
  177. _archiveHandler = archiveHandler;
  178. _directoryPath = directoryPath;
  179. NFile::NName::NormalizeDirPathPrefix(_directoryPath);
  180. }
  181. STDMETHODIMP CArchiveExtractCallback::SetTotal(UInt64 /* size */)
  182. {
  183. return S_OK;
  184. }
  185. STDMETHODIMP CArchiveExtractCallback::SetCompleted(const UInt64 * /* completeValue */)
  186. {
  187. return S_OK;
  188. }
  189. STDMETHODIMP CArchiveExtractCallback::GetStream(UInt32 index,
  190. ISequentialOutStream **outStream, Int32 askExtractMode)
  191. {
  192. *outStream = 0;
  193. _outFileStream.Release();
  194. {
  195. // Get Name
  196. NCOM::CPropVariant propVariant;
  197. RINOK(_archiveHandler->GetProperty(index, kpidPath, &propVariant));
  198. UString fullPath;
  199. if(propVariant.vt == VT_EMPTY)
  200. fullPath = kEmptyFileAlias;
  201. else
  202. {
  203. if(propVariant.vt != VT_BSTR)
  204. return E_FAIL;
  205. fullPath = propVariant.bstrVal;
  206. }
  207. _filePath = fullPath;
  208. }
  209. if (askExtractMode != NArchive::NExtract::NAskMode::kExtract)
  210. return S_OK;
  211. {
  212. // Get Attributes
  213. NCOM::CPropVariant propVariant;
  214. RINOK(_archiveHandler->GetProperty(index, kpidAttributes, &propVariant));
  215. if (propVariant.vt == VT_EMPTY)
  216. {
  217. _processedFileInfo.Attributes = 0;
  218. _processedFileInfo.AttributesAreDefined = false;
  219. }
  220. else
  221. {
  222. if (propVariant.vt != VT_UI4)
  223. throw "incorrect item";
  224. _processedFileInfo.Attributes = propVariant.ulVal;
  225. _processedFileInfo.AttributesAreDefined = true;
  226. }
  227. }
  228. RINOK(IsArchiveItemFolder(_archiveHandler, index, _processedFileInfo.IsDirectory));
  229. {
  230. // Get Modified Time
  231. NCOM::CPropVariant propVariant;
  232. RINOK(_archiveHandler->GetProperty(index, kpidLastWriteTime, &propVariant));
  233. _processedFileInfo.UTCLastWriteTimeIsDefined = false;
  234. switch(propVariant.vt)
  235. {
  236. case VT_EMPTY:
  237. // _processedFileInfo.UTCLastWriteTime = _utcLastWriteTimeDefault;
  238. break;
  239. case VT_FILETIME:
  240. _processedFileInfo.UTCLastWriteTime = propVariant.filetime;
  241. _processedFileInfo.UTCLastWriteTimeIsDefined = true;
  242. break;
  243. default:
  244. return E_FAIL;
  245. }
  246. }
  247. {
  248. // Get Size
  249. NCOM::CPropVariant propVariant;
  250. RINOK(_archiveHandler->GetProperty(index, kpidSize, &propVariant));
  251. bool newFileSizeDefined = (propVariant.vt != VT_EMPTY);
  252. UInt64 newFileSize;
  253. if (newFileSizeDefined)
  254. newFileSize = ConvertPropVariantToUInt64(propVariant);
  255. }
  256. {
  257. // Create folders for file
  258. int slashPos = _filePath.ReverseFind(WCHAR_PATH_SEPARATOR);
  259. if (slashPos >= 0)
  260. NFile::NDirectory::CreateComplexDirectory(_directoryPath + _filePath.Left(slashPos));
  261. }
  262. UString fullProcessedPath = _directoryPath + _filePath;
  263. _diskFilePath = fullProcessedPath;
  264. if (_processedFileInfo.IsDirectory)
  265. {
  266. NFile::NDirectory::CreateComplexDirectory(fullProcessedPath);
  267. }
  268. else
  269. {
  270. NFile::NFind::CFileInfoW fileInfo;
  271. if(NFile::NFind::FindFile(fullProcessedPath, fileInfo))
  272. {
  273. if (!NFile::NDirectory::DeleteFileAlways(fullProcessedPath))
  274. {
  275. PrintString(UString(kCantDeleteOutputFile) + fullProcessedPath);
  276. return E_ABORT;
  277. }
  278. }
  279. _outFileStreamSpec = new COutFileStream;
  280. CMyComPtr<ISequentialOutStream> outStreamLoc(_outFileStreamSpec);
  281. if (!_outFileStreamSpec->Open(fullProcessedPath, CREATE_ALWAYS))
  282. {
  283. PrintString((UString)L"can not open output file " + fullProcessedPath);
  284. return E_ABORT;
  285. }
  286. _outFileStream = outStreamLoc;
  287. *outStream = outStreamLoc.Detach();
  288. }
  289. return S_OK;
  290. }
  291. STDMETHODIMP CArchiveExtractCallback::PrepareOperation(Int32 askExtractMode)
  292. {
  293. _extractMode = false;
  294. switch (askExtractMode)
  295. {
  296. case NArchive::NExtract::NAskMode::kExtract:
  297. _extractMode = true;
  298. };
  299. switch (askExtractMode)
  300. {
  301. case NArchive::NExtract::NAskMode::kExtract:
  302. PrintString(kExtractingString);
  303. break;
  304. case NArchive::NExtract::NAskMode::kTest:
  305. PrintString(kTestingString);
  306. break;
  307. case NArchive::NExtract::NAskMode::kSkip:
  308. PrintString(kSkippingString);
  309. break;
  310. };
  311. PrintString(_filePath);
  312. return S_OK;
  313. }
  314. STDMETHODIMP CArchiveExtractCallback::SetOperationResult(Int32 operationResult)
  315. {
  316. switch(operationResult)
  317. {
  318. case NArchive::NExtract::NOperationResult::kOK:
  319. break;
  320. default:
  321. {
  322. NumErrors++;
  323. PrintString(" ");
  324. switch(operationResult)
  325. {
  326. case NArchive::NExtract::NOperationResult::kUnSupportedMethod:
  327. PrintString(kUnsupportedMethod);
  328. break;
  329. case NArchive::NExtract::NOperationResult::kCRCError:
  330. PrintString(kCRCFailed);
  331. break;
  332. case NArchive::NExtract::NOperationResult::kDataError:
  333. PrintString(kDataError);
  334. break;
  335. default:
  336. PrintString(kUnknownError);
  337. }
  338. }
  339. }
  340. if (_outFileStream != NULL)
  341. {
  342. if (_processedFileInfo.UTCLastWriteTimeIsDefined)
  343. _outFileStreamSpec->SetLastWriteTime(&_processedFileInfo.UTCLastWriteTime);
  344. RINOK(_outFileStreamSpec->Close());
  345. }
  346. _outFileStream.Release();
  347. if (_extractMode && _processedFileInfo.AttributesAreDefined)
  348. NFile::NDirectory::MySetFileAttributes(_diskFilePath, _processedFileInfo.Attributes);
  349. PrintNewLine();
  350. return S_OK;
  351. }
  352. STDMETHODIMP CArchiveExtractCallback::CryptoGetTextPassword(BSTR *password)
  353. {
  354. if (!PasswordIsDefined)
  355. {
  356. // You can ask real password here from user
  357. // Password = GetPassword(OutStream);
  358. // PasswordIsDefined = true;
  359. PrintError("Password is not defined");
  360. return E_ABORT;
  361. }
  362. CMyComBSTR tempName(Password);
  363. *password = tempName.Detach();
  364. return S_OK;
  365. }
  366. //////////////////////////////////////////////////////////////
  367. // Archive Creating callback class
  368. struct CDirItem
  369. {
  370. UInt32 Attributes;
  371. FILETIME CreationTime;
  372. FILETIME LastAccessTime;
  373. FILETIME LastWriteTime;
  374. UInt64 Size;
  375. UString Name;
  376. UString FullPath;
  377. bool IsDirectory() const { return (Attributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ; }
  378. };
  379. class CArchiveUpdateCallback:
  380. public IArchiveUpdateCallback2,
  381. public ICryptoGetTextPassword2,
  382. public CMyUnknownImp
  383. {
  384. public:
  385. MY_UNKNOWN_IMP2(IArchiveUpdateCallback2, ICryptoGetTextPassword2)
  386. // IProgress
  387. STDMETHOD(SetTotal)(UInt64 size);
  388. STDMETHOD(SetCompleted)(const UInt64 *completeValue);
  389. // IUpdateCallback2
  390. STDMETHOD(EnumProperties)(IEnumSTATPROPSTG **enumerator);
  391. STDMETHOD(GetUpdateItemInfo)(UInt32 index,
  392. Int32 *newData, Int32 *newProperties, UInt32 *indexInArchive);
  393. STDMETHOD(GetProperty)(UInt32 index, PROPID propID, PROPVARIANT *value);
  394. STDMETHOD(GetStream)(UInt32 index, ISequentialInStream **inStream);
  395. STDMETHOD(SetOperationResult)(Int32 operationResult);
  396. STDMETHOD(GetVolumeSize)(UInt32 index, UInt64 *size);
  397. STDMETHOD(GetVolumeStream)(UInt32 index, ISequentialOutStream **volumeStream);
  398. STDMETHOD(CryptoGetTextPassword2)(Int32 *passwordIsDefined, BSTR *password);
  399. public:
  400. CRecordVector<UInt64> VolumesSizes;
  401. UString VolName;
  402. UString VolExt;
  403. UString DirPrefix;
  404. const CObjectVector<CDirItem> *DirItems;
  405. bool PasswordIsDefined;
  406. UString Password;
  407. bool AskPassword;
  408. bool m_NeedBeClosed;
  409. UStringVector FailedFiles;
  410. CRecordVector<HRESULT> FailedCodes;
  411. CArchiveUpdateCallback(): PasswordIsDefined(false), AskPassword(false), DirItems(0) {};
  412. ~CArchiveUpdateCallback() { Finilize(); }
  413. HRESULT Finilize();
  414. void Init(const CObjectVector<CDirItem> *dirItems)
  415. {
  416. DirItems = dirItems;
  417. m_NeedBeClosed = false;
  418. FailedFiles.Clear();
  419. FailedCodes.Clear();
  420. }
  421. };
  422. STDMETHODIMP CArchiveUpdateCallback::SetTotal(UInt64 /* size */)
  423. {
  424. return S_OK;
  425. }
  426. STDMETHODIMP CArchiveUpdateCallback::SetCompleted(const UInt64 * /* completeValue */)
  427. {
  428. return S_OK;
  429. }
  430. STDMETHODIMP CArchiveUpdateCallback::EnumProperties(IEnumSTATPROPSTG ** /* enumerator */)
  431. {
  432. return E_NOTIMPL;
  433. }
  434. STDMETHODIMP CArchiveUpdateCallback::GetUpdateItemInfo(UInt32 /* index */,
  435. Int32 *newData, Int32 *newProperties, UInt32 *indexInArchive)
  436. {
  437. if(newData != NULL)
  438. *newData = BoolToInt(true);
  439. if(newProperties != NULL)
  440. *newProperties = BoolToInt(true);
  441. if(indexInArchive != NULL)
  442. *indexInArchive = UInt32(-1);
  443. return S_OK;
  444. }
  445. STDMETHODIMP CArchiveUpdateCallback::GetProperty(UInt32 index, PROPID propID, PROPVARIANT *value)
  446. {
  447. NWindows::NCOM::CPropVariant propVariant;
  448. if (propID == kpidIsAnti)
  449. {
  450. propVariant = false;
  451. propVariant.Detach(value);
  452. return S_OK;
  453. }
  454. {
  455. const CDirItem &dirItem = (*DirItems)[index];
  456. switch(propID)
  457. {
  458. case kpidPath:
  459. propVariant = dirItem.Name;
  460. break;
  461. case kpidIsFolder:
  462. propVariant = dirItem.IsDirectory();
  463. break;
  464. case kpidSize:
  465. propVariant = dirItem.Size;
  466. break;
  467. case kpidAttributes:
  468. propVariant = dirItem.Attributes;
  469. break;
  470. case kpidLastAccessTime:
  471. propVariant = dirItem.LastAccessTime;
  472. break;
  473. case kpidCreationTime:
  474. propVariant = dirItem.CreationTime;
  475. break;
  476. case kpidLastWriteTime:
  477. propVariant = dirItem.LastWriteTime;
  478. break;
  479. }
  480. }
  481. propVariant.Detach(value);
  482. return S_OK;
  483. }
  484. HRESULT CArchiveUpdateCallback::Finilize()
  485. {
  486. if (m_NeedBeClosed)
  487. {
  488. PrintNewLine();
  489. m_NeedBeClosed = false;
  490. }
  491. return S_OK;
  492. }
  493. static void GetStream2(const wchar_t *name)
  494. {
  495. PrintString("Compressing ");
  496. if (name[0] == 0)
  497. name = kEmptyFileAlias;
  498. PrintString(name);
  499. }
  500. STDMETHODIMP CArchiveUpdateCallback::GetStream(UInt32 index, ISequentialInStream **inStream)
  501. {
  502. RINOK(Finilize());
  503. const CDirItem &dirItem = (*DirItems)[index];
  504. GetStream2(dirItem.Name);
  505. if(dirItem.IsDirectory())
  506. return S_OK;
  507. {
  508. CInFileStream *inStreamSpec = new CInFileStream;
  509. CMyComPtr<ISequentialInStream> inStreamLoc(inStreamSpec);
  510. UString path = DirPrefix + dirItem.FullPath;
  511. if(!inStreamSpec->Open(path))
  512. {
  513. DWORD sysError = ::GetLastError();
  514. FailedCodes.Add(sysError);
  515. FailedFiles.Add(path);
  516. // if (systemError == ERROR_SHARING_VIOLATION)
  517. {
  518. PrintNewLine();
  519. PrintError("WARNING: can't open file");
  520. // PrintString(NError::MyFormatMessageW(systemError));
  521. return S_FALSE;
  522. }
  523. // return sysError;
  524. }
  525. *inStream = inStreamLoc.Detach();
  526. }
  527. return S_OK;
  528. }
  529. STDMETHODIMP CArchiveUpdateCallback::SetOperationResult(Int32 /* operationResult */)
  530. {
  531. m_NeedBeClosed = true;
  532. return S_OK;
  533. }
  534. STDMETHODIMP CArchiveUpdateCallback::GetVolumeSize(UInt32 index, UInt64 *size)
  535. {
  536. if (VolumesSizes.Size() == 0)
  537. return S_FALSE;
  538. if (index >= (UInt32)VolumesSizes.Size())
  539. index = VolumesSizes.Size() - 1;
  540. *size = VolumesSizes[index];
  541. return S_OK;
  542. }
  543. STDMETHODIMP CArchiveUpdateCallback::GetVolumeStream(UInt32 index, ISequentialOutStream **volumeStream)
  544. {
  545. wchar_t temp[32];
  546. ConvertUInt64ToString(index + 1, temp);
  547. UString res = temp;
  548. while (res.Length() < 2)
  549. res = UString(L'0') + res;
  550. UString fileName = VolName;
  551. fileName += L'.';
  552. fileName += res;
  553. fileName += VolExt;
  554. COutFileStream *streamSpec = new COutFileStream;
  555. CMyComPtr<ISequentialOutStream> streamLoc(streamSpec);
  556. if(!streamSpec->Create(fileName, false))
  557. return ::GetLastError();
  558. *volumeStream = streamLoc.Detach();
  559. return S_OK;
  560. }
  561. STDMETHODIMP CArchiveUpdateCallback::CryptoGetTextPassword2(Int32 *passwordIsDefined, BSTR *password)
  562. {
  563. if (!PasswordIsDefined)
  564. {
  565. if (AskPassword)
  566. {
  567. // You can ask real password here from user
  568. // Password = GetPassword(OutStream);
  569. // PasswordIsDefined = true;
  570. PrintError("Password is not defined");
  571. return E_ABORT;
  572. }
  573. }
  574. *passwordIsDefined = BoolToInt(PasswordIsDefined);
  575. CMyComBSTR tempName(Password);
  576. *password = tempName.Detach();
  577. return S_OK;
  578. }
  579. //////////////////////////////////////////////////////////////////////////
  580. // Main function
  581. int
  582. #ifdef _MSC_VER
  583. __cdecl
  584. #endif
  585. main(int argc, char* argv[])
  586. {
  587. #ifdef _WIN32
  588. #ifndef _UNICODE
  589. g_IsNT = IsItWindowsNT();
  590. #endif
  591. #endif
  592. PrintStringLn(kCopyrightString);
  593. if (argc < 3)
  594. {
  595. PrintStringLn(kHelpString);
  596. return 1;
  597. }
  598. NWindows::NDLL::CLibrary library;
  599. if (!library.Load(TEXT(kDllName)))
  600. {
  601. PrintError("Can not load library");
  602. return 1;
  603. }
  604. CreateObjectFunc createObjectFunc = (CreateObjectFunc)library.GetProcAddress("CreateObject");
  605. if (createObjectFunc == 0)
  606. {
  607. PrintError("Can not get CreateObject");
  608. return 1;
  609. }
  610. AString command = argv[1];
  611. UString archiveName = GetUnicodeString(argv[2], CP_OEMCP);
  612. if (command.CompareNoCase("a") == 0)
  613. {
  614. // create archive command
  615. if (argc < 4)
  616. {
  617. PrintStringLn(kHelpString);
  618. return 1;
  619. }
  620. CObjectVector<CDirItem> dirItems;
  621. int i;
  622. for (i = 3; i < argc; i++)
  623. {
  624. CDirItem item;
  625. UString name = GetUnicodeString(argv[i], CP_OEMCP);
  626. NFile::NFind::CFileInfoW fileInfo;
  627. if (!NFile::NFind::FindFile(name, fileInfo))
  628. {
  629. PrintString(UString(L"Can't find file") + name);
  630. return 1;
  631. }
  632. item.Attributes = fileInfo.Attributes;
  633. item.Size = fileInfo.Size;
  634. item.CreationTime = fileInfo.CreationTime;
  635. item.LastAccessTime = fileInfo.LastAccessTime;
  636. item.LastWriteTime = fileInfo.LastWriteTime;
  637. item.Name = name;
  638. item.FullPath = name;
  639. dirItems.Add(item);
  640. }
  641. COutFileStream *outFileStreamSpec = new COutFileStream;
  642. CMyComPtr<IOutStream> outFileStream = outFileStreamSpec;
  643. if (!outFileStreamSpec->Create(archiveName, false))
  644. {
  645. PrintError("can't create archive file");
  646. return 1;
  647. }
  648. CMyComPtr<IOutArchive> outArchive;
  649. if (createObjectFunc(&CLSID_CFormat7z, &IID_IOutArchive, (void **)&outArchive) != S_OK)
  650. {
  651. PrintError("Can not get class object");
  652. return 1;
  653. }
  654. CArchiveUpdateCallback *updateCallbackSpec = new CArchiveUpdateCallback;
  655. CMyComPtr<IArchiveUpdateCallback2> updateCallback(updateCallbackSpec);
  656. updateCallbackSpec->Init(&dirItems);
  657. // updateCallbackSpec->PasswordIsDefined = true;
  658. // updateCallbackSpec->Password = L"1";
  659. HRESULT result = outArchive->UpdateItems(outFileStream, dirItems.Size(), updateCallback);
  660. updateCallbackSpec->Finilize();
  661. if (result != S_OK)
  662. {
  663. PrintError("Update Error");
  664. return 1;
  665. }
  666. for (i = 0; i < updateCallbackSpec->FailedFiles.Size(); i++)
  667. {
  668. PrintNewLine();
  669. PrintString((UString)L"Error for file: " + updateCallbackSpec->FailedFiles[i]);
  670. }
  671. if (updateCallbackSpec->FailedFiles.Size() != 0)
  672. return 1;
  673. }
  674. else
  675. {
  676. if (argc != 3)
  677. {
  678. PrintStringLn(kHelpString);
  679. return 1;
  680. }
  681. bool listCommand;
  682. if (command.CompareNoCase("l") == 0)
  683. listCommand = true;
  684. else if (command.CompareNoCase("x") == 0)
  685. listCommand = false;
  686. else
  687. {
  688. PrintError("incorrect command");
  689. return 1;
  690. }
  691. CMyComPtr<IInArchive> archive;
  692. if (createObjectFunc(&CLSID_CFormat7z, &IID_IInArchive, (void **)&archive) != S_OK)
  693. {
  694. PrintError("Can not get class object");
  695. return 1;
  696. }
  697. CInFileStream *fileSpec = new CInFileStream;
  698. CMyComPtr<IInStream> file = fileSpec;
  699. if (!fileSpec->Open(archiveName))
  700. {
  701. PrintError("Can not open archive file");
  702. return 1;
  703. }
  704. {
  705. CArchiveOpenCallback *openCallbackSpec = new CArchiveOpenCallback;
  706. CMyComPtr<IArchiveOpenCallback> openCallback(openCallbackSpec);
  707. openCallbackSpec->PasswordIsDefined = false;
  708. // openCallbackSpec->PasswordIsDefined = true;
  709. // openCallbackSpec->Password = L"1";
  710. if (archive->Open(file, 0, openCallback) != S_OK)
  711. {
  712. PrintError("Can not open archive");
  713. return 1;
  714. }
  715. }
  716. if (listCommand)
  717. {
  718. // List command
  719. UInt32 numItems = 0;
  720. archive->GetNumberOfItems(&numItems);
  721. for (UInt32 i = 0; i < numItems; i++)
  722. {
  723. {
  724. // Get uncompressed size of file
  725. NWindows::NCOM::CPropVariant propVariant;
  726. archive->GetProperty(i, kpidSize, &propVariant);
  727. UString s = ConvertPropVariantToString(propVariant);
  728. PrintString(s);
  729. PrintString(" ");
  730. }
  731. {
  732. // Get name of file
  733. NWindows::NCOM::CPropVariant propVariant;
  734. archive->GetProperty(i, kpidPath, &propVariant);
  735. UString s = ConvertPropVariantToString(propVariant);
  736. PrintString(s);
  737. }
  738. PrintString("\n");
  739. }
  740. }
  741. else
  742. {
  743. // Extract command
  744. CArchiveExtractCallback *extractCallbackSpec = new CArchiveExtractCallback;
  745. CMyComPtr<IArchiveExtractCallback> extractCallback(extractCallbackSpec);
  746. extractCallbackSpec->Init(archive, L""); // second parameter is output folder path
  747. extractCallbackSpec->PasswordIsDefined = false;
  748. // extractCallbackSpec->PasswordIsDefined = true;
  749. // extractCallbackSpec->Password = L"1";
  750. HRESULT result = archive->Extract(NULL, (UInt32)(Int32)(-1), false, extractCallback);
  751. if (result != S_OK)
  752. {
  753. PrintError("Extract Error");
  754. return 1;
  755. }
  756. }
  757. }
  758. return 0;
  759. }