本帖最后由 happy886rr 于 2017-5-4 20:21 编辑
[易压2.0版] 增加 批量解压、批量压缩、单文件压缩、过滤空目录等新功能。一键易压,无声无息。
下载地址: https://pan.baidu.com/s/1geWEDZt
借助开源zlib写了一个zip高速压缩解压软件,实现右键一键压缩解压。程序会自动判断是否为压缩文件,智能的自动选择压缩或是解压。由于是无窗命令行,在控制台下亦可直接被批处理调用。
GUI下调用说明,直接双击ezip.exe就完成了易压的安装,右键查看zip包会有E-Zip的右键菜单和图标。所有压缩解压都是一键的,非常易捷。
命令行下调用说明:- Usage: ezip [argv] ...
- ezip [待解压zip文件]
- ezip [待压缩目录]
复制代码 经测试易压的zip压缩解压速度均高于winRAR,对于zip类型的文件有非常大的压缩优势,且不会弹出任何窗口消耗系统资源。
源码,请配合zlib库、MFC、ATL库编译。- #include "stdafx.h"
- #include "unzip.h"
- #include "zip.h"
-
- using namespace std;
-
- // 定义控制台启动方式
- #pragma comment(linker, "/subsystem:\"windows\" /entry:\"wmainCRTStartup\"")
-
- //判断注册表键值是否存在
- BOOL RegValueExist(HKEY hMainKey, LPCTSTR pSubKey, LPCTSTR pValName)
- {
- BOOL ret =FALSE;
- DWORD dwType = REG_SZ;
- HKEY hKey;
- LSTATUS nRes = RegOpenKeyEx(hMainKey, pSubKey, 0, KEY_READ, &hKey);
- if (nRes != ERROR_SUCCESS)
- {
- return FALSE;
- }
- nRes = RegQueryValueEx(hKey, pValName, NULL, &dwType, NULL, NULL);
- RegCloseKey(hKey);
- if (nRes == ERROR_SUCCESS || nRes ==ERROR_MORE_DATA)
- {
- ret = TRUE;
- }
- return ret;
- }
-
- // 注册右键快捷键值
- int REGRightKeyValue()
- {
- // 获取可执行文件全路径
- TCHAR szFilePath[MAX_PATH + 1];
- GetModuleFileName(NULL, szFilePath, MAX_PATH);
-
- CString csPathBuf=_T("\"") + (CString)szFilePath + _T("\"");
- CString csCommandBuf=_T("\"") + (CString)szFilePath + _T("\"") + _T(" ") + _T("\"%1\"");
-
- // 创建注册表键值
- HKEY hKey;
- DWORD dwDisposition;
-
- RegCreateKeyEx(HKEY_CLASSES_ROOT, _T("*\\shell\\E-Zip"), 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hKey, &dwDisposition);
- RegSetValueEx(hKey, _T("icon"), 0, REG_SZ, (BYTE*)(LPCTSTR)csPathBuf, csPathBuf.GetLength()*sizeof(TCHAR));
-
- RegCreateKeyEx(HKEY_CLASSES_ROOT, _T("*\\shell\\E-Zip\\command"), 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hKey, &dwDisposition);
- RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE*)(LPCTSTR)csCommandBuf, csCommandBuf.GetLength()*sizeof(TCHAR));
-
- RegCreateKeyEx(HKEY_CLASSES_ROOT, _T("Folder\\shell\\E-Zip"), 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hKey, &dwDisposition);
- RegSetValueEx(hKey, _T("icon"), 0, REG_SZ, (BYTE*)(LPCTSTR)csPathBuf, csPathBuf.GetLength()*sizeof(TCHAR));
-
- RegCreateKeyEx(HKEY_CLASSES_ROOT, _T("Folder\\shell\\E-Zip\\command"), 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hKey, &dwDisposition);
- RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE*)(LPCTSTR)csCommandBuf, csCommandBuf.GetLength()*sizeof(TCHAR));
-
- RegCloseKey(hKey);
-
- return 0;
- }
-
- // 从压缩包解压文件
- ZRESULT ExtractZipToDir(LPCTSTR lpszZipFullName, CStringArray& szFilePathArr, LPCTSTR lpszUnZipPath)
- {
- TCHAR buffer[MAX_PATH] = {0};
- CString strUnZipPath = lpszUnZipPath;
- DWORD zResult = ZR_OK;
-
- if (!strUnZipPath.IsEmpty())
- {
- SHCreateDirectoryEx(NULL, lpszUnZipPath, NULL);
- }
- else
- {
- GetCurrentDirectory(MAX_PATH, (LPTSTR)&buffer);
- strUnZipPath = buffer;
- SHCreateDirectoryEx(NULL, strUnZipPath, NULL);
- }
-
- HZIP hz = OpenZip(lpszZipFullName, 0);
- ZIPENTRY ze;
-
- GetZipItem(hz, -1, &ze);
- int numitems = ze.index;
-
- for (int zi = 0; zi < numitems; zi++)
- {
- ZIPENTRY ze;
- GetZipItem(hz,zi,&ze);
- zResult = UnzipItem(hz, zi, (CString)strUnZipPath+_T("\\")+ze.name);
- szFilePathArr.Add(ze.name);
-
- if (zResult != ZR_OK)
- {
- #ifndef _UNICODE
- if (_access(szFilePathArr[zi], 0))
- {
- return zResult;
- }
-
- #else
- if (_access((char *)(LPTSTR)(LPCTSTR)szFilePathArr[zi], 0))
- {
- return zResult;
- }
-
- #endif
- }
- }
-
- CloseZip(hz);
- return zResult;
- }
-
- // 递归子目录到zip文件
- ZRESULT RecursiveSubdirToZip(HZIP zf, const CString& strPath, const CString& parentDir)
- {
- CString strRelativePath;
- CFileFind finder;
- BOOL bWorking = finder.FindFile(strPath + _T("\\*.*"));
-
- while(bWorking)
- {
- bWorking = finder.FindNextFile();
- if(finder.IsDots())
- {
- continue;
- }
-
- if (parentDir == _T(""))
- {
- strRelativePath = finder.GetFileName();
- }
- else
- {
- //生成在zip文件中的相对路径
- strRelativePath = parentDir + _T("\\") + finder.GetFileName();
- }
-
- if(finder.IsDirectory())
- {
- //在zip文件中生成目录结构
- ZipAdd(zf, strRelativePath, NULL);
-
- //递归收集子目录文件
- RecursiveSubdirToZip(zf, finder.GetFilePath(), strRelativePath);
- continue;
- }
-
- //将文件添加到zip文件中
- ZipAdd(zf, strRelativePath, finder.GetFilePath());
- }
- return ZR_OK;
- }
-
- // 从目录创建zip压缩文件
- ZRESULT CompressPathToZip(const CString& dirName, const CString& zipFileName)
- {
- //创建zip文件
- HZIP newZipFile = CreateZip(zipFileName, 0);
-
- if (newZipFile == NULL)
- {
- _tprintf(_T("Can not create zip file !\n"));
- return ZR_FAILED;
- }
-
- RecursiveSubdirToZip(newZipFile, dirName, _T(""));
-
- //关闭zip文件
- CloseZip(newZipFile);
- return ZR_OK;
- }
-
- // 从文件创建zip压缩文件
- ZRESULT CompressFileToZip(LPCTSTR lpszSrcFile, LPCTSTR lpszZipName)
- {
- CString m_s=lpszSrcFile;
- HZIP hz = CreateZip(lpszZipName, 0);
-
- int ind=m_s.ReverseFind(_T('\\'));
-
- if(ind==-1)
- {
- m_s=lpszSrcFile;
- }
- else
- {
- m_s = m_s.Right(m_s.GetLength()-1-ind);
- }
-
- DWORD zResult = ZipAdd(hz, m_s, (CString)lpszSrcFile);
-
- if(zResult == ZR_OK)
- {
- CloseZip(hz);
- }
- return zResult;
- }
-
- // 唯一的应用程序对象
- CWinApp theApp;
-
- int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
- {
- int ezipMODE=0;
- CString m_InFullName;
-
- if(argc==1)
- {
- REGRightKeyValue();
- }
-
- if(argc==2)
- {
- m_InFullName=argv[1];
-
- TCHAR* tpstr=_tcsrchr(argv[1], _T('.'));
- if(tpstr && _tcsicmp(tpstr, _T(".zip"))==0)
- {
- ezipMODE=1;
- }
- else if(PathIsDirectory(m_InFullName))
- {
- ezipMODE=2;
- }
- else
- {
- ezipMODE=3;
- }
- }
- else
- {
- //_tprintf(_T("\nUsage: ezip [option] [parameter] [parameter]\n -x [source file] [extract path]\n -z [source path] [compression name]\n\n"));
- return 1;
- }
-
- int nRetCode = 0;
-
- HMODULE hModule = GetModuleHandle(NULL);
-
- if (hModule != NULL)
- {
- // 初始化 MFC 并在失败时显示错误
- if (!AfxWinInit(hModule, NULL, GetCommandLine(), 0))
- {
- // TODO: 更改错误代码以符合您的需要
- //_tprintf(_T("Error: MFC failed to initialize .\n"));
- nRetCode = 1;
- }
- else
- {
- //根据ezipMODE值执行相应函数
- if(ezipMODE==1)
- {
- CStringArray tmpNAME;
- ExtractZipToDir(m_InFullName, tmpNAME, m_InFullName.Left(m_InFullName.GetLength()-4));
- }
- else if(ezipMODE==2)
- {
- CompressPathToZip(m_InFullName, m_InFullName + _T(".zip"));
- }
- else if(ezipMODE==3)
- {
- CompressFileToZip(m_InFullName, m_InFullName + _T(".zip"));
- }
- }
- }
- else
- {
- // TODO: 更改错误代码以符合您的需要
- //_tprintf(_T("Error: get module handle failed .\n"));
- nRetCode = 1;
- }
-
- return nRetCode;
- }
复制代码
|