第三方函數、頭文件、測試工程下載地址:http://pan.baidu.com/s/1gSfKo
namespace ZipUtils
{
// ------------------------------------------------------------------------------------------------------------------------
// Summary:
// 解壓zip文件到指定路徑, 并返回解壓文件路徑和文件名。
// Parameters:
// lpszZipFullName - 待解壓 zip壓縮包所在文件夾路徑和zip壓縮名稱; 如"D://00//1.zip"。
// szFilePathArr - 保存的解壓后文件的文件名;如"1.jpg"。
// lpszUnZipPath - 解壓出來的文件 所存放位置的完整路徑; 如 “D://01”
// 此參數省略時,默認解壓到exe程序所在文件夾下。
// Returns:
// 解壓成功返回ZR_OK,解壓失敗返回錯誤碼。
// ------------------------------------------------------------------------------------------------------------------------
ZRESULT ExtractZipToDir(LPCTSTR lpszZipFullName, CStringArray& szFilePathArr, LPCTSTR lpszUnZipPath = NULL);
// ------------------------------------------------------------------------------------------------------------------------
// Summary:
// 壓縮指定路徑下的文件,并保存壓縮包到指定路徑。
// Parameters:
// lpszSrcPath - 待壓縮文件所在的路徑; 如"D://00"。
// lpszDestPath - 壓縮完成后,存放壓縮包的路徑。
// 此參數省略時,默認存放路徑為exe程序所在文件的路徑。
// lpszZipName - 壓縮完成后,壓縮的名稱;如“MySkin.zip”。
// Returns:
// 壓縮成功返回ZR_OK,壓縮失敗返回錯誤碼。
// ------------------------------------------------------------------------------------------------------------------------
ZRESULT CompressDirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, LPCTSTR lpszDestPath = NULL);
}
namespace ZipUtils
{
// 全局變量
int g_nCount = 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("http://")+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;
}
ZRESULT DirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, HZIP& hz, LPCTSTR lpszDestPath)
{
static CString strFileName;
g_nCount++;
DWORD zResult = ZR_OK;
TCHAR buffer[MAX_PATH] = {0};
CString strDestPath = lpszDestPath;
if (g_nCount == 1)
{
// 這里邊的只執行一次
if (!strDestPath.IsEmpty())
{
// 如果解壓路徑文件夾不存在 先創建,存在 不做任何修改
SHCreateDirectoryEx(NULL, lpszDestPath, NULL);
}
else
{
GetCurrentDirectory(MAX_PATH, (LPTSTR)&buffer);
strDestPath = buffer;
SHCreateDirectoryEx(NULL, strDestPath, NULL);
}
hz = CreateZip((CString)strDestPath+_T("http://")+(CString)lpszZipName, 0);
}
HANDLE file;
WIN32_FIND_DATA fileData;
file = FindFirstFile((CString)lpszSrcPath+_T("http://*.*"), &fileData);
FindNextFile(file, &fileData);
while (FindNextFile(file, &fileData))
{
// 如果是一個文件目錄
if(fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (strFileName.IsEmpty())
{
ZipAddFolder(hz, fileData.cFileName);
}
else
{
ZipAddFolder(hz, strFileName+_T("http://")+fileData.cFileName);
}
strFileName = fileData.cFileName;
// 存在子文件夾 遞歸調用
DirToZip((CString)lpszSrcPath+_T("http://")+ fileData.cFileName, lpszZipName, hz, lpszDestPath);
strFileName.Empty();
}
else
{
CString strTempPath;
strTempPath.Format(_T("%s//%s"), (CString)lpszSrcPath, fileData.cFileName);
if (strFileName.IsEmpty())
{
ZipAdd(hz, fileData.cFileName, strTempPath);
}
else
{
ZipAdd(hz, strFileName+_T("http://")+fileData.cFileName, strTempPath);
}
if (zResult != ZR_OK)
{
return zResult;
}
}
}
return zResult;
}
ZRESULT CompressDirToZip(LPCTSTR lpszSrcPath, LPCTSTR lpszZipName, LPCTSTR lpszDestPath)
{
HZIP hz;
DWORD zResult = ZR_OK;
zResult = DirToZip(lpszSrcPath, lpszZipName,hz, lpszDestPath);
if(zResult == ZR_OK)
{
CloseZip(hz);
}
g_nCount = 0;
return zResult;
}
}
新聞熱點
疑難解答
圖片精選