// testDlg.cpp : 实现文件 // #include "stdafx.h" #include "test.h" #include "testDlg.h" #include "afxdialogex.h" #ifdef _DEBUG #define new DEBUG_NEW #endif /************************************************************************/ /* 函数:[6/5/2017 IT]; /* 描述:; /* 参数:; /* [IN] :; /* [OUT] :; /* [IN/OUT] :; /* 返回:void; /* 注意: /* fb: set number of fast bytes [5,273],default:128; /* lc: set number of literal context bits [0,8],default:3 /* lp: set number of literal pos bits [0,4],default:0 /* pb: set number of pos bits [0,4],default:2 /* 示例:; /* /* 修改:; /* 日期:; /* 内容:; /************************************************************************/ // 用于应用程序“关于”菜单项的 CAboutDlg 对话框 class CAboutDlg : public CDialogEx { public: CAboutDlg(); // 对话框数据 enum { IDD = IDD_ABOUTBOX }; protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 // 实现 protected: DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) { } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) END_MESSAGE_MAP() // CtestDlg 对话框 CtestDlg::CtestDlg(CWnd* pParent /*=NULL*/) : CDialogEx(CtestDlg::IDD, pParent) { m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CtestDlg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CtestDlg, CDialogEx) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(BTN_CHOOSE_COMPRESS, &CtestDlg::OnBnClickedChooseCompress) ON_BN_CLICKED(BTN_CHOOSE_UNCOMPRESS, &CtestDlg::OnBnClickedChooseUncompress) ON_BN_CLICKED(BTN_COMPRESS, &CtestDlg::OnBnClickedCompress) ON_BN_CLICKED(BTN_UNCOMPRESS, &CtestDlg::OnBnClickedUncompress) END_MESSAGE_MAP() // CtestDlg 消息处理程序 BOOL CtestDlg::OnInitDialog() { CDialogEx::OnInitDialog(); // 将“关于...”菜单项添加到系统菜单中。 // IDM_ABOUTBOX 必须在系统命令范围内。 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { BOOL bNameValid; CString strAboutMenu; bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); ASSERT(bNameValid); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 SetIcon(m_hIcon, TRUE); // 设置大图标 SetIcon(m_hIcon, FALSE); // 设置小图标 #ifndef USE_LIB LoadlzmaLibarary(); #endif #if 1 // 循环本地文件; filehelpImpl ffh; STR_VEC vec_files; STR_VEC vec_names; STR_VEC vec_paths; TCHAR szCurrentDirectory[MAX_PATH] = {0}; DWORD dwRet = GetCurrentDirectory(MAX_PATH, szCurrentDirectory); if ( dwRet == 0 || dwRet > MAX_PATH ) return FALSE; CString strSelf; strSelf.Format(_T("%s\\test_Lib.exe"),szCurrentDirectory ); __p__pgmptr; ffh.getfiles_findout_subfolder(szCurrentDirectory, _T("*.exe|*.dll"), &vec_files); for ( STR_VEC::iterator it = vec_files.begin(); it != vec_files.end(); it++) { if ( strSelf.CompareNoCase(it->c_str()) !=0) CompressFile(it->c_str()); } #endif // TODO: 在此添加额外的初始化代码 #if 0 // 较简单; FILE* pFile = NULL; fopen_s(&pFile,"F:\\创建CMFCOutLookBar.txt", "rb"); if (pFile == NULL) { //printf(stderr, _T("Error to Open the file!")); return FALSE; } fseek(pFile, 0, SEEK_END); size_t srcLen = ftell(pFile); rewind(pFile); size_t destLen = srcLen * 2; unsigned char* psrcRead = new unsigned char[srcLen]; //原始文件数据 unsigned char* pDecomress = new unsigned char[srcLen]; //存放解压缩数据 unsigned char* pLzma = new unsigned char[destLen]; //存放压缩数据 fread(psrcRead, sizeof(char), srcLen, pFile); unsigned char prop[5] = {0}; size_t sizeProp = 5; if (SZ_OK != LzmaCompress(pLzma, &destLen, psrcRead, srcLen, prop, &sizeProp, 9, (1 << 24), 3, 0, 2, 32, 2)) { //出错了 //_ftprintf(stderr, _T("压缩时出错!")); delete[] psrcRead; delete[] pDecomress; delete[] pLzma; fclose(pFile); return FALSE; } FILE* pCompressFile = NULL; fopen_s(&pCompressFile, "compress.dat", "wb"); //写入压缩后的数据 if (pCompressFile == NULL) { //_ftprintf(stderr, _T("创建文件出错!")); delete[] psrcRead; delete[] pDecomress; delete[] pLzma; fclose(pFile); return FALSE; } fwrite(pLzma, sizeof(char), destLen, pCompressFile); fclose(pCompressFile); FILE* pDecompressFile = NULL; fopen_s(&pDecompressFile, "decompress.txt", "wb"); //写入解压缩数据 if (pDecompressFile == NULL) { //_ftprintf(stderr, _T("写入数据出错!")); delete[] psrcRead; delete[] pDecomress; delete[] pLzma; fclose(pFile); return FALSE; } //注意:解压缩时props参数要使用压缩时生成的outProps,这样才能正常解压缩 if (SZ_OK != LzmaUncompress(pDecomress, &srcLen, pLzma, &destLen, prop, 5)) { delete[] psrcRead; delete[] pDecomress; delete[] pLzma; fclose(pDecompressFile); fclose(pFile); return FALSE; } fwrite(pDecomress, sizeof(char), srcLen, pDecompressFile); delete[] psrcRead; delete[] pDecomress; delete[] pLzma; fclose(pDecompressFile); fclose(pFile); #else #if 0 ////////////////////////////////////////////////////////////////////////// // 压缩文件; ////////////////////////////////////////////////////////////////////////// byte prop[5] = {0}; size_t nPropSize = 5; FILE* fp = fopen("D:\\QRcode.zip", "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nSrcLen; byte* pDesData = (byte*)malloc(nDesLen); int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, 1<<24, 3, 0, 2, 32, 2); if ( SZ_ERROR_OUTPUT_EOF == nRet ) {//申请内存不足; free(pDesData); nDesLen += nSrcLen; pDesData = (byte*)malloc(nDesLen); nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); } if ( SZ_OK == nRet ) {//压缩完成; fp = fopen("D:\\des_7z.dat", "wb+"); if ( NULL != fp ) { fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } } free(pDesData); free(pSrcData); } ////////////////////////////////////////////////////////////////////////// // 解压文件; ////////////////////////////////////////////////////////////////////////// fp = fopen("D:\\des_7z.dat", "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = 1.1*nSrcLen + 16*1024; byte* pDesData = (byte*)malloc(nDesLen); int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); if ( SZ_ERROR_INPUT_EOF == nRet ) {//申请空间不足; free(pDesData); pDesData = (byte*)malloc(nDesLen); nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); } if ( SZ_OK == nRet ) {//解压完成; fp = fopen("D:\\out.chm", "wb+"); if ( NULL != fp ) { fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } } free(pSrcData); free(pDesData); } #endif #endif return TRUE; // 除非将焦点设置到控件,否则返回 TRUE } void CtestDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialogEx::OnSysCommand(nID, lParam); } } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序, // 这将由框架自动完成。 void CtestDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // 用于绘制的设备上下文 SendMessage(WM_ICONERASEBKGND, reinterpret_cast(dc.GetSafeHdc()), 0); // 使图标在工作区矩形中居中 int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // 绘制图标 dc.DrawIcon(x, y, m_hIcon); } else { CDialogEx::OnPaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标 //显示。 HCURSOR CtestDlg::OnQueryDragIcon() { return static_cast(m_hIcon); } BOOL OpenFloder(IN CString &strFiles, IN CString strDeftExt, IN CString strTitle, IN CString strFilters) { // 获取桌面路径; //TCHAR szDesktop[MAX_PATH]; //if (SHGetSpecialFolderPath(0, szDesktop, CSIDL_RESOURCES, 0) == FALSE) //{ // return FALSE; //} CString strFindFile = _T(""); DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ENABLESIZING; CFileDialog dlg(TRUE, strDeftExt, NULL, dwFlags, strFilters, NULL); dlg.m_ofn.lpstrTitle = strTitle; // 显示的标题, 应该做成参数传入; dlg.m_ofn.lpstrInitialDir = NULL; // 默认打开的文件夹, 应该做成参数传入; // 显示文件对话框,获得文件名集合; if (dlg.DoModal() == IDCANCEL) return FALSE; strFiles = dlg.GetPathName(); return TRUE; } void CtestDlg::OnBnClickedChooseCompress() { CString strCompressFile; if (OpenFloder(strCompressFile, _T("default Files (*.*)"), _T("请选择要压缩的文件"), _T("所有文件 (*.*)|*.*||"))) { SetDlgItemText(ET_COMPRESS, strCompressFile); } } void CtestDlg::OnBnClickedChooseUncompress() { CString strUnCompressFile; if (OpenFloder(strUnCompressFile, _T("default Files (*.*)"), _T("请选择要解压缩的文件"), _T("所有文件 (*.*)|*.*||"))) { SetDlgItemText(ET_UNCOMPRESS, strUnCompressFile); } } void CtestDlg::OnBnClickedCompress() { CString strCompressFile; GetDlgItemText(ET_COMPRESS, strCompressFile); ////////////////////////////////////////////////////////////////////////// // 压缩文件; ////////////////////////////////////////////////////////////////////////// byte prop[5] = {0}; size_t nPropSize = 5; ULONGLONG ulTickCount = GetTickCount64(); CString strLama = strCompressFile.Mid(0,strCompressFile.ReverseFind('.')) + ".lam"; FILE* fp = fopen(strCompressFile, "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nSrcLen; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2); #else int nRet = g_PtrCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif if ( SZ_ERROR_OUTPUT_EOF == nRet ) {//申请内存不足; free(pDesData); nDesLen += nSrcLen; pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 5, (1<<24), 8, 0, 2, 32, 2); #else nRet = g_PtrCompress(pDesData, &nDesLen, pSrcData, nSrcLen, prop, &nPropSize, 9, (1<<24), 3, 0, 2, 32, 2); #endif } if ( SZ_OK == nRet ) {//压缩完成,保存文件; fp = fopen(strLama, "wb+"); if ( NULL != fp ) { // 写入prop; fwrite(prop, 5, 1, fp); // 写入nPropSize; fwrite(&nPropSize, sizeof(size_t), 1, fp); // 写入文件大小; fwrite(&nSrcLen, sizeof(size_t), 1, fp); // 写入压缩数据; fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } } free(pDesData); free(pSrcData); } ulTickCount = GetTickCount64() - ulTickCount; strLama.Format("耗时:%ld 秒", ulTickCount/1000); AfxMessageBox(strLama); } void CtestDlg::OnBnClickedUncompress() { CString strUnCompressFile; GetDlgItemText(ET_UNCOMPRESS, strUnCompressFile); ////////////////////////////////////////////////////////////////////////// // 解压文件; ////////////////////////////////////////////////////////////////////////// byte prop[5] = {0}; size_t nPropSize = 5; size_t nOrgSize = 0; ULONGLONG ulTickCount = GetTickCount64(); CString strLama = strUnCompressFile.Mid(0,strUnCompressFile.ReverseFind('.')) + ".unlam"; FILE* fp = fopen(strUnCompressFile, "rb"); if ( NULL != fp ) { fseek(fp, 0, SEEK_END); size_t nSrcLen = ftell(fp) - 5 - sizeof(size_t); fseek(fp, 0, SEEK_SET); byte* pSrcData = (byte*)malloc(nSrcLen) ; // 获取prop; fread(prop, 5, 1, fp); // 获取nPropSize; fseek(fp, 5, SEEK_SET); fread(&nPropSize, sizeof(size_t), 1, fp); // 获取原文件大小; fseek(fp, 5 + sizeof(size_t), SEEK_SET); fread(&nOrgSize, sizeof(size_t), 1, fp); // 读取压缩内容; fseek(fp, 5 + 2*sizeof(size_t), SEEK_SET); fread(pSrcData, nSrcLen, 1, fp); fclose(fp); size_t nDesLen = nOrgSize;//1.1*nSrcLen + 16*1024; byte* pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB int nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else int nRet = g_PtrUnCompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif if ( SZ_ERROR_INPUT_EOF == nRet ) {//申请空间不足; free(pDesData); pDesData = (byte*)malloc(nDesLen); #ifdef USE_LIB nRet = LzmaUncompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #else nRet = g_PtrUnCompress(pDesData, &nDesLen, pSrcData, &nSrcLen, prop, nPropSize); #endif } if ( SZ_OK == nRet ) {//解压完成; fp = fopen(strLama, "wb+"); if ( NULL != fp ) { fwrite(pDesData, nDesLen, 1, fp); fclose(fp); } } free(pSrcData); free(pDesData); } ulTickCount = GetTickCount64() - ulTickCount; strLama.Format("耗时:%ld 秒", ulTickCount/1000); AfxMessageBox(strLama); }