国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
使用VC++壓縮解壓縮文件夾

 

前言  項目中要用到一個壓縮解壓縮的模塊, 看了很多文章和源代碼, 
都不是很稱心, 現(xiàn)在把我自己實現(xiàn)的代碼和大家分享.

要求:

 

       1.使用Unicode(支持中文).

       2.使用源代碼.(不使用靜態(tài)或者動態(tài)庫)

       3.實現(xiàn)文件夾壓縮解壓縮即可.(不提供單文件壓縮和內(nèi)存壓縮)

       4.壓縮格式為ZIP.

       5.具有一定的容錯能力.(判斷用戶輸入的內(nèi)容)

 

代碼如下:

*********************ZipImplement.h********************


 1
///////////////////////////////////////////////////////////////////////////// 
 2
// 文件名: <ZipImplement.h> 
 3
// 創(chuàng)建者: <hwfly> 
 4
// 創(chuàng)建日期: 2009-09-27 下午 04:51:46 
 5
// 
 6
// 說明:壓縮解壓縮地圖文件夾 
 7
///////////////////////////////////////////////////////////////////////////// 
 8

 9
#pragma once 
10

11
#include 
"zip.h" 
12
#include 
"unzip.h" 
13

14
class CZipImplement 
15

16
public
17
    CZipImplement(
void); 
18
    
~CZipImplement(void); 
19

20
private
21
    HZIP hz;          
//Zip文件句柄 
22
    ZRESULT zr;    //操作返回值 
23
    ZIPENTRY ze;  //Zip文件入口 
24

25
    CString m_FolderPath;     
//folder路徑 
26
    CString  m_FolderName;  //folder將要被壓縮的文件夾名 
27

28
private
29
    
//實現(xiàn)遍歷文件夾 
30
    void BrowseFile(CString &strFile); 
31

32
    
//獲取相對路徑 
33
    void GetRelativePath(CString& pFullPath, CString& pSubString); 
34

35
    
//創(chuàng)建路徑 
36
    BOOL CreatedMultipleDirectory(wchar_t* direct); 
37
    
///
38
    //*********************************************************************** 
39
    
//* 函數(shù): TransCStringToTCHAR 
40
    
//* 描述:將CString 轉(zhuǎn)換為 TCHAR* 
41
    
//*********************************************************************** 
42
    
//*/ 
43
    
//TCHAR* CString2TCHAR(CString &str) 
44
    
//
45
    
//    int iLen = str.GetLength(); 
46
    
//    TCHAR* szRs = new TCHAR[iLen]; 
47
    
//    lstrcpy(szRs, str.GetBuffer(iLen)); 
48
    
//    str.ReleaseBuffer(); 
49
    
//    return szRs; 
50
    
//
51

52
public
53
    
//壓縮文件夾接口 
54
    BOOL Zip_PackFiles(CString& pFilePath, CString& mZipFileFullPath); 
55

56
    
//解壓縮文件夾接口 
57
    BOOL Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath); 
58

59
public
60
    
//靜態(tài)方法提供文件夾路徑檢查 
61
    static BOOL FolderExist(CString& strPath); 
62
}
;


*********************ZipImplement.cpp*********************


  1
///////////////////////////////////////////////////////////////////////////// 
  2
// 文件名: <ZipImplement.cpp> 
  3
// 創(chuàng)建者: <hwfly> 
  4
// 創(chuàng)建日期: 2009-09-27 下午 04:51:46 
  5
// 
  6
// 說明:壓縮解壓縮地圖文件夾 
  7
///////////////////////////////////////////////////////////////////////////// 
  8

  9
#include 
"StdAfx.h" 
 10
#include 
"zipimplement.h" 
 11
#include 
<direct.h> 
 12
#include 
<vector> 
 13
#include 
<xstring> 
 14

 15
CZipImplement::CZipImplement(
void
 16

 17
}
 
 18

 19
CZipImplement::
~CZipImplement(void
 20

 21
}
 
 22

 23
///////////////////////////////////////////////////////////////////////////// 
 24
// 函數(shù)說明: 實現(xiàn)壓縮文件夾操作 
 25
// 參數(shù)說明: [in]: pFilePath         要被壓縮的文件夾 
 26
//                         mZipFileFullPath  壓縮后的文件名和路徑 
 27
// 返回值: 參數(shù)有誤的情況下返回FALSE,壓縮成功后返回TRUE 
 28
// 函數(shù)作者: <hwfly> 
 29
// 創(chuàng)建日期: 2009-09-27 下午 04:58:52 
 30
///////////////////////////////////////////////////////////////////////////// 
 31
BOOL CZipImplement::Zip_PackFiles(CString& pFilePath, CString& mZipFileFullPath) 
 32

 33
    
//參數(shù)錯誤 
 34
    if ((pFilePath == L""|| (mZipFileFullPath == L"")) 
 35
    

 36
        
//路徑異常返回 
 37
        return FALSE ; 
 38
    }
 
 39

 40
    
if(!CZipImplement::FolderExist(pFilePath)) 
 41
    

 42
        
//要被壓縮的文件夾不存在 
 43
        return FALSE ; 
 44
    }
 
 45

 46
    CString tZipFilePath 
= mZipFileFullPath.Left(mZipFileFullPath.ReverseFind('\\'+ 1); 
 47
    
if(!CZipImplement::FolderExist(tZipFilePath)) 
 48
    

 49
        
//ZIP文件存放的文件夾不存在創(chuàng)建它 
 50
        wchar_t* temp=tZipFilePath.GetBuffer(tZipFilePath.GetLength()); 
 51
        
if (FALSE == CreatedMultipleDirectory(temp)) 
 52
        

 53
            
//創(chuàng)建目錄失敗 
 54
            return FALSE; 
 55
        }
 
 56
    }
 
 57

 58
    
//獲得文件夾的名字 
 59
    if(pFilePath.Right(1== L"\\"
 60
    

 61
        
this->m_FolderPath = pFilePath.Left(pFilePath.GetLength() - 1); 
 62
        m_FolderName 
= m_FolderPath.Right(m_FolderPath.GetLength() - m_FolderPath.ReverseFind('\\'- 1); 
 63
    }
 
 64
    
else 
 65
    

 66
        
this->m_FolderPath = pFilePath; 
 67
        m_FolderName 
= pFilePath.Right(pFilePath.GetLength() - pFilePath.ReverseFind('\\'- 1); 
 68
    }
 
 69

 70
    
/************************************************************************/ 
 71

 72
    
//創(chuàng)建ZIP文件 
 73
    hz=CreateZip(mZipFileFullPath,0); 
 74
    
if(hz == 0
 75
    

 76
        
//創(chuàng)建Zip文件失敗 
 77
        return FALSE; 
 78
    }
 
 79

 80
    
//遞歸文件夾,將獲取的問價加入ZIP文件 
 81
    BrowseFile(pFilePath); 
 82
    
//關(guān)閉ZIP文件 
 83
    CloseZip(hz); 
 84

 85
    
/************************************************************************/ 
 86

 87
    CFileFind tFFind; 
 88
    
if (!tFFind.FindFile(mZipFileFullPath)) 
 89
    

 90
        
//壓縮失敗(未發(fā)現(xiàn)壓縮后的文件) 
 91
        return FALSE; 
 92
    }
 
 93

 94
    
return TRUE; 
 95
}
 
 96

 97
///////////////////////////////////////////////////////////////////////////// 
 98
// 函數(shù)說明: 解壓縮文件夾 
 99
// 參數(shù)說明: [in]: mUnPackPath     解壓后文件存放的路徑 
100
//                         mZipFileFullPath  ZIP文件的路徑 
101
// 返回值: 
102
// 函數(shù)作者: <hwfly> 
103
// 創(chuàng)建日期: 2009-09-27 上午 11:04:28 
104
///////////////////////////////////////////////////////////////////////////// 
105
BOOL CZipImplement::Zip_UnPackFiles(CString &mZipFileFullPath, CString& mUnPackPath) 
106

107
    
//參數(shù)錯誤 
108
    if ((mUnPackPath == L""|| (mZipFileFullPath == L"")) 
109
    

110
        
//路徑異常返回 
111
        return FALSE ; 
112
    }
 
113

114
    CFileFind tFFind; 
115
    
if (!tFFind.FindFile(mZipFileFullPath)) 
116
    

117
        
//壓縮失敗(未發(fā)現(xiàn)壓縮文件) 
118
        return FALSE; 
119
    }
 
120

121
    
//如果解壓縮的路徑不存在 試圖創(chuàng)建它 
122
    CString tZipFilePath = mUnPackPath; 
123
    
if(!CZipImplement::FolderExist(tZipFilePath)) 
124
    

125
        
//解壓后存放的文件夾不存在 創(chuàng)建它 
126
        wchar_t* temp=tZipFilePath.GetBuffer(tZipFilePath.GetLength()); 
127
        
if (FALSE == CreatedMultipleDirectory(temp)) 
128
        

129
            
//創(chuàng)建目錄失敗 
130
            return FALSE; 
131
        }
 
132
    }
 
133
    
/************************************************************************/ 
134
    
//打開ZIP文件 
135
    hz=OpenZip(mZipFileFullPath,0); 
136
    
if(hz == 0
137
    

138
        
//打開Zip文件失敗 
139
        return FALSE; 
140
    }
 
141

142
    zr
=SetUnzipBaseDir(hz,mUnPackPath); 
143
    
if(zr != ZR_OK) 
144
    

145
        
//打開Zip文件失敗 
146
        CloseZip(hz); 
147
        
return FALSE;       
148
    }
 
149

150
    zr
=GetZipItem(hz,-1,&ze); 
151
    
if(zr != ZR_OK) 
152
    

153
        
//獲取Zip文件內(nèi)容失敗 
154
        CloseZip(hz); 
155
        
return FALSE;       
156
    }
 
157

158
    
int numitems=ze.index; 
159
    
for (int i=0; i<numitems; i++
160
    

161
        zr
=GetZipItem(hz,i,&ze); 
162
        zr
=UnzipItem(hz,i,ze.name); 
163

164
        
if(zr != ZR_OK) 
165
        

166
            
//獲取Zip文件內(nèi)容失敗 
167
            CloseZip(hz); 
168
            
return FALSE;       
169
        }
 
170
    }
 
171

172
    CloseZip(hz); 
173
    
return TRUE; 
174
}
 
175

176
///////////////////////////////////////////////////////////////////////////// 
177
// 函數(shù)說明: 檢查指定的文件夾是否存在 
178
// 參數(shù)說明: [in]:strPath 檢查的文件夾 (此方法會主動向路徑末尾添加*.*) 
179
// 返回值:BOOL類型,存在返回TRUE,否則為FALSE 
180
// 函數(shù)作者: <hwfly> 
181
// 創(chuàng)建日期: 2009-09-27 下午 02:16:36 
182
///////////////////////////////////////////////////////////////////////////// 
183
BOOL CZipImplement::FolderExist(CString& strPath) 
184

185
    CString sCheckPath 
= strPath; 
186

187
    
if(sCheckPath.Right(1!= L"\\"
188
        sCheckPath 
+= L"\\"
189

190
    sCheckPath 
+= L"*.*"
191

192
    WIN32_FIND_DATA wfd; 
193
    BOOL rValue 
= FALSE; 
194

195
    HANDLE hFind 
= FindFirstFile(sCheckPath, &wfd); 
196

197
    
if ((hFind!=INVALID_HANDLE_VALUE) && 
198
        (wfd.dwFileAttributes
&FILE_ATTRIBUTE_DIRECTORY) || (wfd.dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE)) 
199
    

200
        
//如果存在并類型是文件夾 
201
        rValue = TRUE; 
202
    }
 
203

204
    FindClose(hFind); 
205
    
return rValue; 
206
}
 
207

208
///////////////////////////////////////////////////////////////////////////// 
209
// 函數(shù)說明: 遍歷文件夾 
210
// 參數(shù)說明: [in]:strFile 遍歷的文件夾(此方法會主動向路徑末尾添加*.*) 
211
// 返回值:BOOL類型,存在返回TRUE,否則為FALSE 
212
// 函數(shù)作者: <hwfly> 
213
// 創(chuàng)建日期: 2009-09-27 下午 02:16:36 
214
///////////////////////////////////////////////////////////////////////////// 
215
void CZipImplement::BrowseFile(CString &strFile) 
216

217
    CFileFind ff; 
218
    CString szDir 
= strFile; 
219

220
    
if(szDir.Right(1!= L"\\"
221
        szDir 
+= L"\\"
222

223
    szDir 
+= L"*.*"
224

225
    BOOL res 
= ff.FindFile(szDir); 
226
    
while(res) 
227
    

228
        res 
= ff.FindNextFile(); 
229
        
if(ff.IsDirectory() && !ff.IsDots()) 
230
        

231
            
//如果是一個子目錄,用遞歸繼續(xù)往深一層找 
232
            CString strPath = ff.GetFilePath(); 
233

234
            CString subPath; 
235
            GetRelativePath(strPath,subPath); 
236
            
//將文件添加到ZIP文件 
237
            ZipAddFolder(hz,subPath); 
238
            BrowseFile(strPath); 
239
        }
 
240
        
else if(!ff.IsDirectory() && !ff.IsDots()) 
241
        

242
            
//顯示當(dāng)前訪問的文件(完整路徑) 
243
            CString strPath = ff.GetFilePath(); 
244
            CString subPath; 
245

246
            GetRelativePath(strPath,subPath); 
247
            
//將文件添加到ZIP文件 
248
            ZipAdd(hz,subPath,strPath); 
249
        }
 
250
    }
 
251

252
    
//關(guān)閉 
253
    ff.Close(); 
254
}
 
255

256
///////////////////////////////////////////////////////////////////////////// 
257
// 函數(shù)說明: 獲取相對路徑 
258
// 參數(shù)說明: [in]:pFullPath 當(dāng)前文件的完整路徑 [out] : 解析后的相對路徑 
259
// 函數(shù)作者: <hwfly> 
260
// 創(chuàng)建日期: 2009-9-28 上午 11:17:21 
261
///////////////////////////////////////////////////////////////////////////// 
262
void CZipImplement::GetRelativePath(CString& pFullPath,CString& pSubString) 
263

264
    pSubString 
= pFullPath.Right(pFullPath.GetLength() - this->m_FolderPath.GetLength() + this->m_FolderName.GetLength()); 
265
}
 
266

267
///////////////////////////////////////////////////////////////////////////// 
268
// 函數(shù)說明: 創(chuàng)建多級目錄 
269
// 參數(shù)說明: [in]: 路徑字符串 
270
// 返回值: BOOL 成功True 失敗False 
271
// 函數(shù)作者: <hwfly> 
272
// 創(chuàng)建日期: 2009-9-28 下午 04:53:20 
273
///////////////////////////////////////////////////////////////////////////// 
274
BOOL CZipImplement::CreatedMultipleDirectory(wchar_t* direct) 
275

276
    std::wstring Directoryname 
= direct; 
277

278
    
if (Directoryname[Directoryname.length() - 1!=  '\\'
279
    

280
        Directoryname.append(
1'\\'); 
281
    }
 
282
    std::vector
< std::wstring> vpath; 
283
    std::wstring strtemp; 
284
    BOOL  bSuccess 
= FALSE; 
285
    
for (int i = 0; i < Directoryname.length(); i++
286
    

287
        
if ( Directoryname[i] != '\\'
288
        

289
            strtemp.append(
1,Directoryname[i]);    
290
        }
 
291
        
else 
292
        

293
            vpath.push_back(strtemp); 
294
            strtemp.append(
1'\\'); 
295
        }
 
296
    }
 
297
    std::vector
<std::wstring>:: const_iterator vIter; 
298
    
for (vIter = vpath.begin();vIter != vpath.end(); vIter++
299
    

300
        bSuccess 
= CreateDirectory(vIter->c_str(), NULL) ? TRUE :FALSE; 
301
    }
 
302

303
    
return bSuccess; 
304
}

 

=====================以上為源代碼=====================

簡單說明:

1.使用VS2003編寫.

2.WinXp sp2下運行測試通過.

3.為了簡化算法,使用了很多MFC提供的函數(shù), 如果要移植到標(biāo)準(zhǔn)C++請重新實現(xiàn)部分函數(shù).

4.壓縮算法采用了ljw1004 這位高手的算法.

5."zip.h" 和 "unzip.h"以及實現(xiàn)請至http://www.codeproject.com/KB/files/zip_utils.aspx 下載, 下載的源文件中有示例程序可以參考. 
    將下載后的 zip.h unzip.h zip.cpp unzip.cpp 添加到自己的項目中.

后記:第一次使用VC++開發(fā)項目,遇到了很多問題,對于相關(guān)的問題和我自己的解決辦法將在以后的文章中給出.


(###)

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
刪除文件夾以及文件夾下的所有文件
zip文件怎么打開
壓縮包文件用什么程序打開
CFileFind用法小結(jié)
VC文件目錄常見操作
VBA實用小程序70:獲取壓縮文件中指定文件的修改日期
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服