// 檔案名稱: ZipFunction.h
#pragma once
#include "zip.h"
#include "unzip.h"
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);
}
#include "stdafx.h"
#include "ZipFunction.h"
#include <io.h>
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("\\")+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("\\")+(CString)lpszZipName, 0);
}
HANDLE file;
WIN32_FIND_DATA fileData;
file = FindFirstFile((CString)lpszSrcPath+_T("\\*.*"), &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("\\")+fileData.cFileName);
}
strFileName = fileData.cFileName;
// 存在子檔案夾 遞迴調用
DirToZip((CString)lpszSrcPath+_T("\\")+ 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("\\")+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;
}
}