Browse Source

Z991239-1017 #comment fea zipDir和zipVector实现

陈良瑜80374463 4 years ago
parent
commit
50f286ad99
1 changed files with 125 additions and 7 deletions
  1. 125 7
      Module/mod_RomoteController/MyZip.cpp

+ 125 - 7
Module/mod_RomoteController/MyZip.cpp

@@ -162,13 +162,131 @@ std::pair<bool, std::string> ZipVector(const std::string &dstFile, const std::ve
 
 }
 #else
-std::pair<bool, std::string> ZipDir(const std::string& dstFile, const std::string& srcDir, void* hz = NULL)
-{
-	return std::make_pair(true, "");
-}
-std::pair<bool, std::string> ZipVector(const std::string& dstFile, const std::vector<std::string>& srcArr)
-{
-	return std::make_pair(true, "");
+#include "XZipZilb.h"
+#include "zip.h"
+#include <boost/filesystem.hpp>
+#include <vector>
+#include <string>
+#include "remoteBase.h"
+#include <memory>
+
+namespace MyZip {
+
+	string g_srcPath;
+
+	void ZipAllFile(string strDir, zipFile hz)
+	{
+		string creDir = strDir.substr(getPathName(g_srcPath).second.length() + 1);
+		if (!AddFileToZip(hz, creDir.c_str(), NULL))
+			return;
+
+		namespace fs = boost::filesystem;
+		std::vector<std::string> ret, name;
+		fs::path fullpath(strDir);
+		fs::recursive_directory_iterator end_iter;
+		for (fs::recursive_directory_iterator iter(fullpath); iter != end_iter; iter++) {
+			try {
+				if (*iter == "." || *iter == "..")
+					continue;
+				else if (fs::is_directory(*iter))
+				{
+					string creDir = iter->path().string().substr(getPathName(g_srcPath).second.length() + 1);
+					if (!AddFileToZip(hz, creDir.c_str(), NULL))
+						return;
+				}
+				else {
+					string curFile = iter->path().string();
+					string zipFile = curFile.substr(getPathName(g_srcPath).second.length() + 1);
+					string tempFile = curFile + ".bak";
+					if (Base_CopyFile(curFile, tempFile))
+					{
+						AddFileToZip(hz, zipFile.c_str(), tempFile.c_str());
+						Base_DeleteFile(tempFile);
+					}
+				}
+			}
+			catch (const std::exception& ex) {
+				std::cerr << ex.what() << std::endl;
+				continue;
+			}
+		}
+	}
+
+
+	std::pair<bool, std::string> ZipDir(const std::string& dstFile, const std::string& srcDir, void* hz)
+	{
+		//1.创建zip
+		zipFile newhz = (zipFile)hz;
+		if (NULL == newhz)
+			newhz = zipOpen(dstFile.c_str(), APPEND_STATUS_CREATE); //创建zip文件
+		if (newhz)
+		{
+			g_srcPath = srcDir.c_str();
+			try
+			{
+				ZipAllFile(g_srcPath, newhz);
+			}
+			catch (exception& e)
+			{
+				return make_pair(false, string("Zip文件失败") + e.what());
+			}
+
+		}
+		else
+			return make_pair(false, "创建压缩文件失败");
+		if (NULL == hz)
+			zipClose(newhz, NULL);
+		return make_pair(true, "");
+	}
+	std::pair<bool, std::string> ZipVector(const std::string& dstFile, const std::vector<std::string>& srcArr)
+	{
+		if (Base_Exist(dstFile))
+			Base_DeleteFile(dstFile);
+
+		for (auto it : srcArr)
+		{
+			if (!Base_Exist(it))
+				return make_pair(false, it + string("文件不存在"));
+		}
+
+		zipFile newhz = zipOpen(dstFile.c_str(), APPEND_STATUS_CREATE); //创建zip文件
+
+		if (!newhz)
+			return make_pair(false, "创建压缩文件失败");
+
+		for (auto it : srcArr)
+		{
+			if (Base_isDirectory(it))
+			{
+				auto dirInfo = getPathName(it);
+				g_srcPath = it;
+				try
+				{
+					ZipAllFile(it, newhz);
+				}
+				catch (exception& e)
+				{
+					zipClose(newhz, NULL);
+					if (Base_Exist(dstFile))
+						Base_DeleteFile(dstFile);
+					return make_pair(false, it + "压缩文件夹失败:" + e.what());
+				}
+			}
+			else
+			{
+				if (!AddFileToZip(newhz, getPathName(it).first.c_str(), it.c_str())) {
+					zipClose(newhz, NULL);
+					if (Base_Exist(dstFile))
+						Base_DeleteFile(dstFile);
+					return make_pair(false, it + "压缩失败");
+				}
+			}
+		}
+		zipClose(newhz, NULL);
+		return make_pair(true, "");
+
+	}
+
 }
 
 #endif