MyZip.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "MyZip.h"
  2. #include <cstring>
  3. #include <windows.h>
  4. #include <winnt.h>
  5. #include "XZip.h"
  6. #include<Shlwapi.h>
  7. #pragma comment(lib, "shlwapi.lib")
  8. using namespace std;
  9. namespace MyZip{
  10. string g_srcPath;
  11. bool existFile(LPCSTR lpFilePath)
  12. {
  13. DWORD dwRest = GetFileAttributesA(lpFilePath);
  14. return dwRest != INVALID_FILE_ATTRIBUTES;
  15. }
  16. void ZipAllFile(string strDir, HZIP hz)
  17. {
  18. if (strDir == "")
  19. return;
  20. HANDLE hFind;
  21. WIN32_FIND_DATA findData;
  22. LARGE_INTEGER size;
  23. string dirNew;
  24. if (strDir[strDir.length() - 1] != '\\')
  25. strDir = strDir + "\\";
  26. dirNew = strDir + "*.*";
  27. hFind = FindFirstFile(dirNew.c_str(), &findData);
  28. do
  29. {
  30. // 是否是文件夹,并且名称不为"."或".."
  31. if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
  32. continue;
  33. else if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
  34. {
  35. // 将dirNew设置为搜索到的目录,并进行下一轮搜索
  36. string curDir = strDir + findData.cFileName;
  37. ZipAllFile(curDir,hz);
  38. }
  39. else
  40. {
  41. string curFile = strDir + findData.cFileName;
  42. string zipFile = curFile.substr(g_srcPath.length());
  43. string tempFile = curFile + ".bak";
  44. if (CopyFileA(curFile.c_str(), tempFile.c_str(), FALSE))
  45. {
  46. ZipAdd(hz, zipFile.c_str(), (void *)tempFile.c_str(), 0, ZIP_FILENAME);
  47. DeleteFileA(tempFile.c_str());
  48. }
  49. }
  50. } while (FindNextFile(hFind, &findData));
  51. FindClose(hFind);
  52. }
  53. std::pair<bool, std::string> ZipDir(const std::string &dstFile, const std::string &srcDir, void *hz)
  54. {
  55. //1.创建zip
  56. HZIP newhz = (HZIP)hz;
  57. if (NULL == newhz)
  58. newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
  59. if (newhz)
  60. {
  61. g_srcPath = srcDir.c_str();
  62. try
  63. {
  64. ZipAllFile(g_srcPath, newhz);
  65. }
  66. catch (exception &e)
  67. {
  68. return make_pair(false, string("Zip文件失败") + e.what());
  69. }
  70. }
  71. else
  72. return make_pair(false, "创建压缩文件失败");
  73. if (NULL == hz)
  74. CloseZip(newhz);
  75. return make_pair(true, "");
  76. }
  77. std::pair<bool, std::string> ZipVector(const std::string &dstFile, const std::vector<std::string> &srcArr)
  78. {
  79. if (existFile(dstFile.c_str()))
  80. DeleteFileA(dstFile.c_str());
  81. for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
  82. {
  83. if (!existFile(i->c_str()))
  84. return make_pair(false, *i + string("文件不存在"));
  85. }
  86. HZIP newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
  87. if (!newhz)
  88. return make_pair(false, "创建压缩文件失败");
  89. std::pair<bool, std::string> result = make_pair(true, "");
  90. for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
  91. {
  92. if (PathIsDirectoryA(i->c_str()))
  93. {
  94. int pos = i->find_last_of('\\');
  95. string dirPath(i->substr(0, pos));
  96. string dirName(i->substr(pos + 1));
  97. //ZipAdd(newhz, dirName.c_str(), 0, 0, ZIP_FOLDER);
  98. g_srcPath = dirPath;
  99. try
  100. {
  101. ZipAllFile(*i, newhz);
  102. }
  103. catch (exception &e)
  104. {
  105. result = make_pair(false, *i + "压缩文件夹失败:" + e.what());
  106. }
  107. }
  108. else
  109. {
  110. int pos = i->find_last_of('\\');
  111. string filename(i->substr(pos + 1));
  112. ZRESULT zr = ZipAdd(newhz, filename.c_str(), (void *)i->c_str(), 0, ZIP_FILENAME);
  113. if (ZR_OK != zr)
  114. {
  115. result = make_pair(false, *i + "压缩失败");
  116. break;
  117. }
  118. }
  119. }
  120. CloseZip(newhz);
  121. if (false == result.first)
  122. {
  123. if (existFile(dstFile.c_str()))
  124. DeleteFileA(dstFile.c_str());
  125. return result;
  126. }
  127. return make_pair(true, "");
  128. }
  129. }