testDlg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // testDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "test.h"
  5. #include "testDlg.h"
  6. #include "afxdialogex.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #endif
  10. /************************************************************************/
  11. /* 函数:[6/5/2017 IT];
  12. /* 描述:;
  13. /* 参数:;
  14. /* [IN] :;
  15. /* [OUT] :;
  16. /* [IN/OUT] :;
  17. /* 返回:void;
  18. /* 注意:
  19. /* fb: set number of fast bytes [5,273],default:128;
  20. /* lc: set number of literal context bits [0,8],default:3
  21. /* lp: set number of literal pos bits [0,4],default:0
  22. /* pb: set number of pos bits [0,4],default:2
  23. /* 示例:;
  24. /*
  25. /* 修改:;
  26. /* 日期:;
  27. /* 内容:;
  28. /************************************************************************/
  29. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  30. class CAboutDlg : public CDialogEx
  31. {
  32. public:
  33. CAboutDlg();
  34. // 对话框数据
  35. enum { IDD = IDD_ABOUTBOX };
  36. protected:
  37. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  38. // 实现
  39. protected:
  40. DECLARE_MESSAGE_MAP()
  41. };
  42. CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
  43. {
  44. }
  45. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  46. {
  47. CDialogEx::DoDataExchange(pDX);
  48. }
  49. BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
  50. END_MESSAGE_MAP()
  51. // CtestDlg 对话框
  52. CtestDlg::CtestDlg(CWnd* pParent /*=NULL*/)
  53. : CDialogEx(CtestDlg::IDD, pParent)
  54. {
  55. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  56. }
  57. void CtestDlg::DoDataExchange(CDataExchange* pDX)
  58. {
  59. CDialogEx::DoDataExchange(pDX);
  60. }
  61. BEGIN_MESSAGE_MAP(CtestDlg, CDialogEx)
  62. ON_WM_SYSCOMMAND()
  63. ON_WM_PAINT()
  64. ON_WM_QUERYDRAGICON()
  65. ON_BN_CLICKED(BTN_CHOOSE_COMPRESS, &CtestDlg::OnBnClickedChooseCompress)
  66. ON_BN_CLICKED(BTN_CHOOSE_UNCOMPRESS, &CtestDlg::OnBnClickedChooseUncompress)
  67. ON_BN_CLICKED(BTN_COMPRESS, &CtestDlg::OnBnClickedCompress)
  68. ON_BN_CLICKED(BTN_UNCOMPRESS, &CtestDlg::OnBnClickedUncompress)
  69. END_MESSAGE_MAP()
  70. // CtestDlg 消息处理程序
  71. BOOL CtestDlg::OnInitDialog()
  72. {
  73. CDialogEx::OnInitDialog();
  74. // 将“关于...”菜单项添加到系统菜单中。
  75. // IDM_ABOUTBOX 必须在系统命令范围内。
  76. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  77. ASSERT(IDM_ABOUTBOX < 0xF000);
  78. CMenu* pSysMenu = GetSystemMenu(FALSE);
  79. if (pSysMenu != NULL)
  80. {
  81. BOOL bNameValid;
  82. CString strAboutMenu;
  83. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  84. ASSERT(bNameValid);
  85. if (!strAboutMenu.IsEmpty())
  86. {
  87. pSysMenu->AppendMenu(MF_SEPARATOR);
  88. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  89. }
  90. }
  91. // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
  92. // 执行此操作
  93. SetIcon(m_hIcon, TRUE); // 设置大图标
  94. SetIcon(m_hIcon, FALSE); // 设置小图标
  95. #ifndef USE_LIB
  96. LoadlzmaLibarary();
  97. #endif
  98. #if 1
  99. // 循环本地文件;
  100. filehelpImpl ffh;
  101. STR_VEC vec_files;
  102. STR_VEC vec_names;
  103. STR_VEC vec_paths;
  104. TCHAR szCurrentDirectory[MAX_PATH] = {0};
  105. DWORD dwRet = GetCurrentDirectory(MAX_PATH, szCurrentDirectory);
  106. if ( dwRet == 0 || dwRet > MAX_PATH )
  107. return FALSE;
  108. CString strSelf;
  109. strSelf.Format(_T("%s\\test_Lib.exe"),szCurrentDirectory );
  110. __p__pgmptr;
  111. ffh.getfiles_findout_subfolder(szCurrentDirectory, _T("*.exe|*.dll"), &vec_files);
  112. for ( STR_VEC::iterator it = vec_files.begin(); it != vec_files.end(); it++)
  113. {
  114. if ( strSelf.CompareNoCase(it->c_str()) !=0)
  115. CompressFile(it->c_str());
  116. }
  117. #endif
  118. // TODO: 在此添加额外的初始化代码
  119. #if 0 // 较简单;
  120. FILE* pFile = NULL;
  121. fopen_s(&pFile,"F:\\创建CMFCOutLookBar.txt", "rb");
  122. if (pFile == NULL)
  123. {
  124. //printf(stderr, _T("Error to Open the file!"));
  125. return FALSE;
  126. }
  127. fseek(pFile, 0, SEEK_END);
  128. size_t srcLen = ftell(pFile);
  129. rewind(pFile);
  130. size_t destLen = srcLen * 2;
  131. unsigned char* psrcRead = new unsigned char[srcLen]; //原始文件数据
  132. unsigned char* pDecomress = new unsigned char[srcLen]; //存放解压缩数据
  133. unsigned char* pLzma = new unsigned char[destLen]; //存放压缩数据
  134. fread(psrcRead, sizeof(char), srcLen, pFile);
  135. unsigned char prop[5] = {0};
  136. size_t sizeProp = 5;
  137. if (SZ_OK != LzmaCompress(pLzma, &destLen, psrcRead, srcLen, prop, &sizeProp, 9, (1 << 24), 3, 0, 2, 32, 2))
  138. {
  139. //出错了
  140. //_ftprintf(stderr, _T("压缩时出错!"));
  141. delete[] psrcRead;
  142. delete[] pDecomress;
  143. delete[] pLzma;
  144. fclose(pFile);
  145. return FALSE;
  146. }
  147. FILE* pCompressFile = NULL;
  148. fopen_s(&pCompressFile, "compress.dat", "wb");
  149. //写入压缩后的数据
  150. if (pCompressFile == NULL)
  151. {
  152. //_ftprintf(stderr, _T("创建文件出错!"));
  153. delete[] psrcRead;
  154. delete[] pDecomress;
  155. delete[] pLzma;
  156. fclose(pFile);
  157. return FALSE;
  158. }
  159. fwrite(pLzma, sizeof(char), destLen, pCompressFile);
  160. fclose(pCompressFile);
  161. FILE* pDecompressFile = NULL;
  162. fopen_s(&pDecompressFile, "decompress.txt", "wb");
  163. //写入解压缩数据
  164. if (pDecompressFile == NULL)
  165. {
  166. //_ftprintf(stderr, _T("写入数据出错!"));
  167. delete[] psrcRead;
  168. delete[] pDecomress;
  169. delete[] pLzma;
  170. fclose(pFile);
  171. return FALSE;
  172. }
  173. //注意:解压缩时props参数要使用压缩时生成的outProps,这样才能正常解压缩
  174. if (SZ_OK != LzmaUncompress(pDecomress, &srcLen, pLzma, &destLen, prop, 5))
  175. {
  176. delete[] psrcRead;
  177. delete[] pDecomress;
  178. delete[] pLzma;
  179. fclose(pDecompressFile);
  180. fclose(pFile);
  181. return FALSE;
  182. }
  183. fwrite(pDecomress, sizeof(char), srcLen, pDecompressFile);
  184. delete[] psrcRead;
  185. delete[] pDecomress;
  186. delete[] pLzma;
  187. fclose(pDecompressFile);
  188. fclose(pFile);
  189. #else
  190. #if 0
  191. //////////////////////////////////////////////////////////////////////////
  192. // 压缩文件;
  193. //////////////////////////////////////////////////////////////////////////
  194. byte prop[5] = {0};
  195. size_t nPropSize = 5;
  196. FILE* fp = fopen("D:\\QRcode.zip", "rb");
  197. if ( NULL != fp )
  198. {
  199. fseek(fp, 0, SEEK_END);
  200. size_t nSrcLen = ftell(fp);
  201. fseek(fp, 0, SEEK_SET);
  202. byte* pSrcData = (byte*)malloc(nSrcLen);
  203. fread(pSrcData, nSrcLen, 1, fp);
  204. fclose(fp);
  205. size_t nDesLen = nSrcLen;
  206. byte* pDesData = (byte*)malloc(nDesLen);
  207. int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, 1<<24, 3, 0, 2, 32, 2);
  208. if ( SZ_ERROR_OUTPUT_EOF == nRet )
  209. {//申请内存不足;
  210. free(pDesData);
  211. nDesLen += nSrcLen;
  212. pDesData = (byte*)malloc(nDesLen);
  213. nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2);
  214. }
  215. if ( SZ_OK == nRet )
  216. {//压缩完成;
  217. fp = fopen("D:\\des_7z.dat", "wb+");
  218. if ( NULL != fp )
  219. {
  220. fwrite(pDesData, nDesLen, 1, fp);
  221. fclose(fp);
  222. }
  223. }
  224. free(pDesData);
  225. free(pSrcData);
  226. }
  227. //////////////////////////////////////////////////////////////////////////
  228. // 解压文件;
  229. //////////////////////////////////////////////////////////////////////////
  230. fp = fopen("D:\\des_7z.dat", "rb");
  231. if ( NULL != fp )
  232. {
  233. fseek(fp, 0, SEEK_END);
  234. size_t nSrcLen = ftell(fp);
  235. fseek(fp, 0, SEEK_SET);
  236. byte* pSrcData = (byte*)malloc(nSrcLen);
  237. fread(pSrcData, nSrcLen, 1, fp);
  238. fclose(fp);
  239. size_t nDesLen = 1.1*nSrcLen + 16*1024;
  240. byte* pDesData = (byte*)malloc(nDesLen);
  241. int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  242. if ( SZ_ERROR_INPUT_EOF == nRet )
  243. {//申请空间不足;
  244. free(pDesData);
  245. pDesData = (byte*)malloc(nDesLen);
  246. nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  247. }
  248. if ( SZ_OK == nRet )
  249. {//解压完成;
  250. fp = fopen("D:\\out.chm", "wb+");
  251. if ( NULL != fp )
  252. {
  253. fwrite(pDesData, nDesLen, 1, fp);
  254. fclose(fp);
  255. }
  256. }
  257. free(pSrcData);
  258. free(pDesData);
  259. }
  260. #endif
  261. #endif
  262. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  263. }
  264. void CtestDlg::OnSysCommand(UINT nID, LPARAM lParam)
  265. {
  266. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  267. {
  268. CAboutDlg dlgAbout;
  269. dlgAbout.DoModal();
  270. }
  271. else
  272. {
  273. CDialogEx::OnSysCommand(nID, lParam);
  274. }
  275. }
  276. // 如果向对话框添加最小化按钮,则需要下面的代码
  277. // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
  278. // 这将由框架自动完成。
  279. void CtestDlg::OnPaint()
  280. {
  281. if (IsIconic())
  282. {
  283. CPaintDC dc(this); // 用于绘制的设备上下文
  284. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  285. // 使图标在工作区矩形中居中
  286. int cxIcon = GetSystemMetrics(SM_CXICON);
  287. int cyIcon = GetSystemMetrics(SM_CYICON);
  288. CRect rect;
  289. GetClientRect(&rect);
  290. int x = (rect.Width() - cxIcon + 1) / 2;
  291. int y = (rect.Height() - cyIcon + 1) / 2;
  292. // 绘制图标
  293. dc.DrawIcon(x, y, m_hIcon);
  294. }
  295. else
  296. {
  297. CDialogEx::OnPaint();
  298. }
  299. }
  300. //当用户拖动最小化窗口时系统调用此函数取得光标
  301. //显示。
  302. HCURSOR CtestDlg::OnQueryDragIcon()
  303. {
  304. return static_cast<HCURSOR>(m_hIcon);
  305. }
  306. BOOL OpenFloder(IN CString &strFiles, IN CString strDeftExt, IN CString strTitle, IN CString strFilters)
  307. {
  308. // 获取桌面路径;
  309. //TCHAR szDesktop[MAX_PATH];
  310. //if (SHGetSpecialFolderPath(0, szDesktop, CSIDL_RESOURCES, 0) == FALSE)
  311. //{
  312. // return FALSE;
  313. //}
  314. CString strFindFile = _T("");
  315. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING;
  316. CFileDialog dlg(TRUE, strDeftExt, NULL, dwFlags, strFilters, NULL);
  317. dlg.m_ofn.lpstrTitle = strTitle; // 显示的标题, 应该做成参数传入;
  318. dlg.m_ofn.lpstrInitialDir = NULL; // 默认打开的文件夹, 应该做成参数传入;
  319. // 显示文件对话框,获得文件名集合;
  320. if (dlg.DoModal() == IDCANCEL)
  321. return FALSE;
  322. strFiles = dlg.GetPathName();
  323. return TRUE;
  324. }
  325. void CtestDlg::OnBnClickedChooseCompress()
  326. {
  327. CString strCompressFile;
  328. if (OpenFloder(strCompressFile, _T("default Files (*.*)"), _T("请选择要压缩的文件"), _T("所有文件 (*.*)|*.*||")))
  329. {
  330. SetDlgItemText(ET_COMPRESS, strCompressFile);
  331. }
  332. }
  333. void CtestDlg::OnBnClickedChooseUncompress()
  334. {
  335. CString strUnCompressFile;
  336. if (OpenFloder(strUnCompressFile, _T("default Files (*.*)"), _T("请选择要解压缩的文件"), _T("所有文件 (*.*)|*.*||")))
  337. {
  338. SetDlgItemText(ET_UNCOMPRESS, strUnCompressFile);
  339. }
  340. }
  341. void CtestDlg::OnBnClickedCompress()
  342. {
  343. CString strCompressFile;
  344. GetDlgItemText(ET_COMPRESS, strCompressFile);
  345. //////////////////////////////////////////////////////////////////////////
  346. // 压缩文件;
  347. //////////////////////////////////////////////////////////////////////////
  348. byte prop[5] = {0};
  349. size_t nPropSize = 5;
  350. ULONGLONG ulTickCount = GetTickCount64();
  351. CString strLama = strCompressFile.Mid(0,strCompressFile.ReverseFind('.')) + ".lam";
  352. FILE* fp = fopen(strCompressFile, "rb");
  353. if ( NULL != fp )
  354. {
  355. fseek(fp, 0, SEEK_END);
  356. size_t nSrcLen = ftell(fp);
  357. fseek(fp, 0, SEEK_SET);
  358. byte* pSrcData = (byte*)malloc(nSrcLen);
  359. fread(pSrcData, nSrcLen, 1, fp);
  360. fclose(fp);
  361. size_t nDesLen = nSrcLen;
  362. byte* pDesData = (byte*)malloc(nDesLen);
  363. #ifdef USE_LIB
  364. int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2);
  365. #else
  366. int nRet = g_PtrCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2);
  367. #endif
  368. if ( SZ_ERROR_OUTPUT_EOF == nRet )
  369. {//申请内存不足;
  370. free(pDesData);
  371. nDesLen += nSrcLen;
  372. pDesData = (byte*)malloc(nDesLen);
  373. #ifdef USE_LIB
  374. nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2);
  375. #else
  376. nRet = g_PtrCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2);
  377. #endif
  378. }
  379. if ( SZ_OK == nRet )
  380. {//压缩完成,保存文件;
  381. fp = fopen(strLama, "wb+");
  382. if ( NULL != fp )
  383. {
  384. // 写入prop;
  385. fwrite(prop, 5, 1, fp);
  386. // 写入nPropSize;
  387. fwrite(&nPropSize, sizeof(size_t), 1, fp);
  388. // 写入文件大小;
  389. fwrite(&nSrcLen, sizeof(size_t), 1, fp);
  390. // 写入压缩数据;
  391. fwrite(pDesData, nDesLen, 1, fp);
  392. fclose(fp);
  393. }
  394. }
  395. free(pDesData);
  396. free(pSrcData);
  397. }
  398. ulTickCount = GetTickCount64() - ulTickCount;
  399. strLama.Format("耗时:%ld 秒", ulTickCount/1000);
  400. AfxMessageBox(strLama);
  401. }
  402. void CtestDlg::OnBnClickedUncompress()
  403. {
  404. CString strUnCompressFile;
  405. GetDlgItemText(ET_UNCOMPRESS, strUnCompressFile);
  406. //////////////////////////////////////////////////////////////////////////
  407. // 解压文件;
  408. //////////////////////////////////////////////////////////////////////////
  409. byte prop[5] = {0};
  410. size_t nPropSize = 5;
  411. size_t nOrgSize = 0;
  412. ULONGLONG ulTickCount = GetTickCount64();
  413. CString strLama = strUnCompressFile.Mid(0,strUnCompressFile.ReverseFind('.')) + ".unlam";
  414. FILE* fp = fopen(strUnCompressFile, "rb");
  415. if ( NULL != fp )
  416. {
  417. fseek(fp, 0, SEEK_END);
  418. size_t nSrcLen = ftell(fp) - 5 - sizeof(size_t);
  419. fseek(fp, 0, SEEK_SET);
  420. byte* pSrcData = (byte*)malloc(nSrcLen) ;
  421. // 获取prop;
  422. fread(prop, 5, 1, fp);
  423. // 获取nPropSize;
  424. fseek(fp, 5, SEEK_SET);
  425. fread(&nPropSize, sizeof(size_t), 1, fp);
  426. // 获取原文件大小;
  427. fseek(fp, 5 + sizeof(size_t), SEEK_SET);
  428. fread(&nOrgSize, sizeof(size_t), 1, fp);
  429. // 读取压缩内容;
  430. fseek(fp, 5 + 2*sizeof(size_t), SEEK_SET);
  431. fread(pSrcData, nSrcLen, 1, fp);
  432. fclose(fp);
  433. size_t nDesLen = nOrgSize;//1.1*nSrcLen + 16*1024;
  434. byte* pDesData = (byte*)malloc(nDesLen);
  435. #ifdef USE_LIB
  436. int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  437. #else
  438. int nRet = g_PtrUnCompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  439. #endif
  440. if ( SZ_ERROR_INPUT_EOF == nRet )
  441. {//申请空间不足;
  442. free(pDesData);
  443. pDesData = (byte*)malloc(nDesLen);
  444. #ifdef USE_LIB
  445. nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  446. #else
  447. nRet = g_PtrUnCompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize);
  448. #endif
  449. }
  450. if ( SZ_OK == nRet )
  451. {//解压完成;
  452. fp = fopen(strLama, "wb+");
  453. if ( NULL != fp )
  454. {
  455. fwrite(pDesData, nDesLen, 1, fp);
  456. fclose(fp);
  457. }
  458. }
  459. free(pSrcData);
  460. free(pDesData);
  461. }
  462. ulTickCount = GetTickCount64() - ulTickCount;
  463. strLama.Format("耗时:%ld 秒", ulTickCount/1000);
  464. AfxMessageBox(strLama);
  465. }