MyZip.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "MyZip.h"
  2. #if (defined _WIN32 || defined _WIN64)
  3. #include <cstring>
  4. #include <windows.h>
  5. #include <winnt.h>
  6. #include "XZip.h"
  7. #include<Shlwapi.h>
  8. #pragma comment(lib, "shlwapi.lib")
  9. using namespace std;
  10. namespace MyZip{
  11. string g_srcPath;
  12. bool existFile(LPCSTR lpFilePath)
  13. {
  14. DWORD dwRest = GetFileAttributesA(lpFilePath);
  15. return dwRest != INVALID_FILE_ATTRIBUTES;
  16. }
  17. void ZipAllFile(string strDir, HZIP hz)
  18. {
  19. if (strDir == "")
  20. return;
  21. HANDLE hFind;
  22. WIN32_FIND_DATA findData;
  23. LARGE_INTEGER size;
  24. string dirNew;
  25. if (strDir[strDir.length() - 1] != '\\')
  26. strDir = strDir + "\\";
  27. dirNew = strDir + "*.*";
  28. hFind = FindFirstFile(dirNew.c_str(), &findData);
  29. do
  30. {
  31. // 是否是文件夹,并且名称不为"."或".."
  32. if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
  33. continue;
  34. else if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
  35. {
  36. // 将dirNew设置为搜索到的目录,并进行下一轮搜索
  37. string curDir = strDir + findData.cFileName;
  38. ZipAllFile(curDir,hz);
  39. }
  40. else
  41. {
  42. string curFile = strDir + findData.cFileName;
  43. string zipFile = curFile.substr(g_srcPath.length());
  44. string tempFile = curFile + ".bak";
  45. if (CopyFileA(curFile.c_str(), tempFile.c_str(), FALSE))
  46. {
  47. ZipAdd(hz, zipFile.c_str(), (void *)tempFile.c_str(), 0, ZIP_FILENAME);
  48. DeleteFileA(tempFile.c_str());
  49. }
  50. }
  51. } while (FindNextFile(hFind, &findData));
  52. FindClose(hFind);
  53. }
  54. std::pair<bool, std::string> ZipDir(const std::string &dstFile, const std::string &srcDir, void *hz)
  55. {
  56. //1.创建zip
  57. HZIP newhz = (HZIP)hz;
  58. if (NULL == newhz)
  59. newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
  60. if (newhz)
  61. {
  62. g_srcPath = srcDir.c_str();
  63. try
  64. {
  65. ZipAllFile(g_srcPath, newhz);
  66. }
  67. catch (exception &e)
  68. {
  69. return make_pair(false, string("Zip文件失败") + e.what());
  70. }
  71. }
  72. else
  73. return make_pair(false, "创建压缩文件失败");
  74. if (NULL == hz)
  75. CloseZip(newhz);
  76. return make_pair(true, "");
  77. }
  78. std::pair<bool, std::string> ZipVector(const std::string &dstFile, const std::vector<std::string> &srcArr)
  79. {
  80. if (existFile(dstFile.c_str()))
  81. DeleteFileA(dstFile.c_str());
  82. for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
  83. {
  84. if (!existFile(i->c_str()))
  85. return make_pair(false, *i + string("文件不存在"));
  86. }
  87. HZIP newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
  88. if (!newhz)
  89. return make_pair(false, "创建压缩文件失败");
  90. std::pair<bool, std::string> result = make_pair(true, "");
  91. for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
  92. {
  93. if (PathIsDirectoryA(i->c_str()))
  94. {
  95. int pos = i->find_last_of('\\');
  96. string dirPath(i->substr(0, pos));
  97. string dirName(i->substr(pos + 1));
  98. //ZipAdd(newhz, dirName.c_str(), 0, 0, ZIP_FOLDER);
  99. g_srcPath = dirPath;
  100. try
  101. {
  102. ZipAllFile(*i, newhz);
  103. }
  104. catch (exception &e)
  105. {
  106. result = make_pair(false, *i + "压缩文件夹失败:" + e.what());
  107. }
  108. }
  109. else
  110. {
  111. int pos = i->find_last_of('\\');
  112. string filename(i->substr(pos + 1));
  113. ZRESULT zr = ZipAdd(newhz, filename.c_str(), (void *)i->c_str(), 0, ZIP_FILENAME);
  114. if (ZR_OK != zr)
  115. {
  116. result = make_pair(false, *i + "压缩失败");
  117. break;
  118. }
  119. }
  120. }
  121. CloseZip(newhz);
  122. if (false == result.first)
  123. {
  124. if (existFile(dstFile.c_str()))
  125. DeleteFileA(dstFile.c_str());
  126. return result;
  127. }
  128. return make_pair(true, "");
  129. }
  130. }
  131. #else
  132. #include "XZipZilb.h"
  133. #include "zip.h"
  134. #include <boost/filesystem.hpp>
  135. #include <vector>
  136. #include <string>
  137. #include "remoteBase.h"
  138. #include <memory>
  139. namespace MyZip {
  140. string g_srcPath;
  141. void ZipAllFile(string strDir, zipFile hz)
  142. {
  143. string creDir = strDir.substr(getPathName(g_srcPath).second.length() + 1);
  144. if (!AddFileToZip(hz, creDir.c_str(), NULL))
  145. return;
  146. namespace fs = boost::filesystem;
  147. std::vector<std::string> ret, name;
  148. fs::path fullpath(strDir);
  149. fs::recursive_directory_iterator end_iter;
  150. for (fs::recursive_directory_iterator iter(fullpath); iter != end_iter; iter++) {
  151. try {
  152. if (*iter == "." || *iter == "..")
  153. continue;
  154. else if (fs::is_directory(*iter))
  155. {
  156. string creDir = iter->path().string().substr(getPathName(g_srcPath).second.length() + 1);
  157. if (!AddFileToZip(hz, creDir.c_str(), NULL))
  158. return;
  159. }
  160. else {
  161. string curFile = iter->path().string();
  162. string zipFile = curFile.substr(getPathName(g_srcPath).second.length() + 1);
  163. string tempFile = curFile + ".bak";
  164. if (Base_CopyFile(curFile, tempFile))
  165. {
  166. AddFileToZip(hz, zipFile.c_str(), tempFile.c_str());
  167. Base_DeleteFile(tempFile);
  168. }
  169. }
  170. }
  171. catch (const std::exception& ex) {
  172. std::cerr << ex.what() << std::endl;
  173. continue;
  174. }
  175. }
  176. }
  177. std::pair<bool, std::string> ZipDir(const std::string& dstFile, const std::string& srcDir, void* hz)
  178. {
  179. //1.创建zip
  180. zipFile newhz = (zipFile)hz;
  181. if (NULL == newhz)
  182. newhz = zipOpen(dstFile.c_str(), APPEND_STATUS_CREATE); //创建zip文件
  183. if (newhz)
  184. {
  185. g_srcPath = srcDir.c_str();
  186. try
  187. {
  188. ZipAllFile(g_srcPath, newhz);
  189. }
  190. catch (exception& e)
  191. {
  192. return make_pair(false, string("Zip文件失败") + e.what());
  193. }
  194. }
  195. else
  196. return make_pair(false, "创建压缩文件失败");
  197. if (NULL == hz)
  198. zipClose(newhz, NULL);
  199. return make_pair(true, "");
  200. }
  201. std::pair<bool, std::string> ZipVector(const std::string& dstFile, const std::vector<std::string>& srcArr)
  202. {
  203. if (Base_Exist(dstFile))
  204. Base_DeleteFile(dstFile);
  205. for (auto it : srcArr)
  206. {
  207. if (!Base_Exist(it))
  208. return make_pair(false, it + string("文件不存在"));
  209. }
  210. zipFile newhz = zipOpen(dstFile.c_str(), APPEND_STATUS_CREATE); //创建zip文件
  211. if (!newhz)
  212. return make_pair(false, "创建压缩文件失败");
  213. for (auto it : srcArr)
  214. {
  215. if (Base_isDirectory(it))
  216. {
  217. auto dirInfo = getPathName(it);
  218. g_srcPath = it;
  219. try
  220. {
  221. ZipAllFile(it, newhz);
  222. }
  223. catch (exception& e)
  224. {
  225. zipClose(newhz, NULL);
  226. if (Base_Exist(dstFile))
  227. Base_DeleteFile(dstFile);
  228. return make_pair(false, it + "压缩文件夹失败:" + e.what());
  229. }
  230. }
  231. else
  232. {
  233. if (!AddFileToZip(newhz, getPathName(it).first.c_str(), it.c_str())) {
  234. zipClose(newhz, NULL);
  235. if (Base_Exist(dstFile))
  236. Base_DeleteFile(dstFile);
  237. return make_pair(false, it + "压缩失败");
  238. }
  239. }
  240. }
  241. zipClose(newhz, NULL);
  242. return make_pair(true, "");
  243. }
  244. }
  245. #endif