使用zlib-1.2.11版本的开源库,实现我需要的对文件或者文件夹的压缩,查阅了一些博客大牛的资料,后面根据自己的需要修改。
下面给出我的代码:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "stdafx.h"
#include < string >
#include < iostream >
#include < vector >
#include < Shlwapi.h >
#include "zip.h"
#include "unzip.h"
#include "zlib.h"
#include < stdio.h >
#include < stdlib.h >
#include < fstream >
#include < sstream >
using namespace std;
//部分头文件不需要(自行去掉)
#pragma comment(lib, "Shlwapi.lib")
bool nyAddfiletoZip(zipFile zfile, const std::string& fileNameinZip, const std::string& srcfile);
bool nyCollectfileInDirtoZip(zipFile zfile, const std::string& filepath, const std::string& parentdirName);
bool nyCreateZipfromDir(const std::string& dirpathName, const std::string& zipfileName, const std::string& parentdirName);
int _tmain(int argc, _TCHAR* argv[])
{
std::string dirpath = "D:\\\\RecycleBin\\\\wei"; //源文件/文件夹
std::string zipfileName = "D:\\\\RecycleBin\\\\lango.rar"; //目的压缩包
nyCreateZipfromDir(dirpath, zipfileName, "wei");
system("pause");
return 0;
}
/*
* 函数功能 :解压zip文件
* 备 注 :参数strFilePath表示zip压缩文件的路径
* 参数strTempPath表示要解压到的文件目录
*/
bool nyAddfiletoZip(zipFile zfile, const std::string& fileNameinZip, const std::string& srcfile)
{
if (NULL == zfile || fileNameinZip.empty()/* || srcfile.empty()为空代表空目录*/)
{
return 0;
}
int nErr = 0;
zip_fileinfo zinfo = {0};
tm_zip tmz = { 0 };
zinfo.tmz_date = tmz;
zinfo.dosDate = 0;
zinfo.internal_fa = 0;
zinfo.external_fa = 0;
char sznewfileName[MAX_PATH] = { 0 };
memset(sznewfileName, 0x00, sizeof(sznewfileName));
strcat_s(sznewfileName, fileNameinZip.c_str());
if (srcfile.empty())
{
strcat_s(sznewfileName, "\\\\");
}
nErr = zipOpenNewFileInZip(zfile, sznewfileName, &zinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
if (nErr != ZIP_OK)
{
return false;
}
if (!srcfile.empty())
{
//打开源文件
FILE* srcfp = _fsopen(srcfile.c_str(), "rb", _SH_DENYNO);
if (NULL == srcfp)
{
std::cout << "Open source file failed." << std::endl;
return false;
}
//读入源文件写入zip文件
int numBytes = 0;
char* pBuf = new char[1024 * 100];
if (NULL == pBuf)
{
std::cout << "new buffer failed." << std::endl;
return 0;
}
while (!feof(srcfp))
{
memset(pBuf, 0x00, sizeof(pBuf));
numBytes = fread(pBuf, 1, sizeof(pBuf), srcfp);
nErr = zipWriteInFileInZip(zfile, pBuf, numBytes);
if (ferror(srcfp))
{
break;
}
}
delete[] pBuf;
fclose(srcfp);
}
zipCloseFileInZip(zfile);
return true;
}
bool nyCollectfileInDirtoZip(zipFile zfile, const std::string& filepath, const std::string& parentdirName)
{
if (NULL == zfile || filepath.empty())
{
return false;
}
bool bFile = false;
std::string relativepath = "";
WIN32_FIND_DATAA findFileData;
char szpath[MAX_PATH] = { 0 };
if (::PathIsDirectoryA(filepath.c_str()))
{
strcpy_s(szpath, sizeof(szpath) / sizeof(szpath[0]), filepath.c_str());
int len = strlen(szpath) + strlen("\\\\*.*") + 1;
strcat_s(szpath, len, "\\\\*.*");
}
else
{
bFile = true;
strcpy_s(szpath, sizeof(szpath) / sizeof(szpath[0]), filepath.c_str());
}
HANDLE hFile = ::FindFirstFileA(szpath, &findFileData);
if (NULL == hFile)
{
return false;
}
do
{
if (parentdirName.empty())
relativepath = findFileData.cFileName;
else
relativepath = parentdirName + "\\\\" + findFileData.cFileName;//生成zip文件中的相对路径
if (findFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
if (strcmp(findFileData.cFileName, ".") != 0 && strcmp(findFileData.cFileName, "..") != 0)
{
nyAddfiletoZip(zfile, relativepath, "");
char szTemp[MAX_PATH] = { 0 };
strcpy_s(szTemp, filepath.c_str());
strcat_s(szTemp, "\\\\");
strcat_s(szTemp, findFileData.cFileName);
nyCollectfileInDirtoZip(zfile, szTemp, relativepath);
}
continue;
}
char szTemp[MAX_PATH] = { 0 };
if (bFile)
{
//注意:处理单独文件的压缩
strcpy_s(szTemp, filepath.c_str());
}
else
{
//注意:处理目录文件的压缩
strcpy_s(szTemp, filepath.c_str());
strcat_s(szTemp, "\\\\");
strcat_s(szTemp, findFileData.cFileName);
}
nyAddfiletoZip(zfile, relativepath, szTemp);
} while (::FindNextFileA(hFile, &findFileData));
FindClose(hFile);
return true;
}
bool nyCreateZipfromDir(const std::string& dirpathName, const std::string& zipfileName, const std::string& parentdirName)
{
bool bRet = false;
/***********参数注释*********/
/*APPEND_STATUS_CREATE 创建追加
APPEND_STATUS_CREATEAFTER 创建后追加(覆盖方式)
APPEND_STATUS_ADDINZIP 直接追加*/
/****************************/
zipFile zFile = NULL;
if (!::PathFileExistsA(zipfileName.c_str()))
{
zFile = zipOpen(zipfileName.c_str(), APPEND_STATUS_CREATE);
}
else
{
zFile = zipOpen(zipfileName.c_str(), APPEND_STATUS_ADDINZIP);
}
if (NULL == zFile)
{
std::cout << "create zip file failed." << std::endl;
return bRet;
}
if (nyCollectfileInDirtoZip(zFile, dirpathName, parentdirName))
{
bRet = true;
}
zipClose(zFile, NULL);
return bRet;
}
|
zlib库自行下载来编译,然后加入到自己的项目里面去,(需要重新编译哦,不顺利的话可能会遇到很多问题哦x_O)。在下刚出自茅庐,不足之处还望指教,相互学习。后面还有解压部分(阅读了一个大神的代码,后来发现了一个严肃的问题,我进行了改正。x_O)到时候再把demo上传吧!
以上这篇C++ 压缩文件及文件夹方法 使用zlib开源库就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 58
-
2025-05-27 62
-
微信公众平台免费建站是否支持自定义模板?如何选择适合的模板?
2025-06-04 66 -
2025-06-04 95
-
thinkphp6使用mysql悲观锁解决商品超卖问题的实现
2025-05-29 46
热门评论