C++解壓zip檔案
/************************************************************************/
/* 對指定資料夾下面的zip壓縮檔案進行解壓
/* sourcePath: zip壓縮包所在路徑
/* targetPath: 解壓後的路徑
/* bNeedDel: 是否刪除源zip壓縮包檔案
/************************************************************************/
bool UnzipFile(char* sourcePath,char * targetPath, bool bNeedDel)
{
ZIPENTRY ze;
CFileFind fileFinder;
CString filePath = CString(sourcePath) + _T("//*.*");
BOOL bFinished = fileFinder.FindFile(filePath);
while(bFinished) //每次迴圈對應一個類別目錄
{
bFinished = fileFinder.FindNextFile();
CString fileName = fileFinder.GetFileName();
int dotPos=fileName.ReverseFind('.');
CString fileExt=fileName.Right(fileName.GetLength()-dotPos);
CString strZipPath = fileName.Mid(0,dotPos);
if(fileExt == _T(".zip")) //若是txt檔案則開始分類測試
{
CString strZipName = CString(sourcePath) + CString("\\") + fileName;
//CString strUnZipPath = CString(targetPath) + CString("\\") + strZipPath;
CString strUnZipPath = CString(targetPath);
if(!PathFileExists(strUnZipPath))
{
CreateDirectory(strUnZipPath,NULL);
}
HZIP hz = OpenZip(strZipName.GetBuffer(0), 0, ZIP_FILENAME);
if (hz)
{
GetZipItem(hz, -1, &ze);
int iCount= ze.index;
for(int i=0; i<iCount; i++)
{
GetZipItem(hz, i, &ze);
CString strZipTargetName = CString(strUnZipPath) + CString("\\") + ze.name;
ZRESULT zr = UnzipItem(hz, i, strZipTargetName.GetBuffer(0), 0, ZIP_FILENAME);
}
}
CloseZip(hz);
}
}
if(bNeedDel)
{
CString filePath = CString(sourcePath) + _T("//*.*");
BOOL bFinished = fileFinder.FindFile(filePath);
while(bFinished) //每次迴圈對應一個類別目錄
{
bFinished = fileFinder.FindNextFile();
CString fileName = fileFinder.GetFileName();
if(fileName.Compare(".")==0)
continue;
if(fileName.Compare("..")==0)
continue;
if(fileName.GetLength()>0)
{
DeleteFile(CString(sourcePath)+CString("\\") + fileName);
}
}
}
fileFinder.Close();
return true;
}