123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- #include "MyZip.h"
- #if (defined _WIN32 || defined _WIN64)
- #include <cstring>
- #include <windows.h>
- #include <winnt.h>
- #include "XZip.h"
- #include<Shlwapi.h>
- #pragma comment(lib, "shlwapi.lib")
- using namespace std;
- namespace MyZip{
- string g_srcPath;
- bool existFile(LPCSTR lpFilePath)
- {
- DWORD dwRest = GetFileAttributesA(lpFilePath);
- return dwRest != INVALID_FILE_ATTRIBUTES;
- }
- void ZipAllFile(string strDir, HZIP hz)
- {
- if (strDir == "")
- return;
- HANDLE hFind;
- WIN32_FIND_DATA findData;
- LARGE_INTEGER size;
- string dirNew;
- if (strDir[strDir.length() - 1] != '\\')
- strDir = strDir + "\\";
- dirNew = strDir + "*.*";
- hFind = FindFirstFile(dirNew.c_str(), &findData);
- do
- {
- // 是否是文件夹,并且名称不为"."或".."
- if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0)
- continue;
- else if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
- {
- // 将dirNew设置为搜索到的目录,并进行下一轮搜索
- string curDir = strDir + findData.cFileName;
- ZipAllFile(curDir,hz);
- }
- else
- {
- string curFile = strDir + findData.cFileName;
- string zipFile = curFile.substr(g_srcPath.length());
- string tempFile = curFile + ".bak";
- if (CopyFileA(curFile.c_str(), tempFile.c_str(), FALSE))
- {
- ZipAdd(hz, zipFile.c_str(), (void *)tempFile.c_str(), 0, ZIP_FILENAME);
- DeleteFileA(tempFile.c_str());
- }
-
-
- }
- } while (FindNextFile(hFind, &findData));
- FindClose(hFind);
- }
- std::pair<bool, std::string> ZipDir(const std::string &dstFile, const std::string &srcDir, void *hz)
- {
- //1.创建zip
- HZIP newhz = (HZIP)hz;
- if (NULL == newhz)
- newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
- 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)
- CloseZip(newhz);
- return make_pair(true, "");
- }
- std::pair<bool, std::string> ZipVector(const std::string &dstFile, const std::vector<std::string> &srcArr)
- {
- if (existFile(dstFile.c_str()))
- DeleteFileA(dstFile.c_str());
- for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
- {
- if (!existFile(i->c_str()))
- return make_pair(false, *i + string("文件不存在"));
- }
- HZIP newhz = CreateZip((void *)dstFile.c_str(), 0, ZIP_FILENAME);
- if (!newhz)
- return make_pair(false, "创建压缩文件失败");
- std::pair<bool, std::string> result = make_pair(true, "");
- for (vector<string>::const_iterator i = srcArr.begin(); i != srcArr.end(); i++)
- {
- if (PathIsDirectoryA(i->c_str()))
- {
- int pos = i->find_last_of('\\');
- string dirPath(i->substr(0, pos));
- string dirName(i->substr(pos + 1));
- //ZipAdd(newhz, dirName.c_str(), 0, 0, ZIP_FOLDER);
- g_srcPath = dirPath;
- try
- {
- ZipAllFile(*i, newhz);
- }
- catch (exception &e)
- {
- result = make_pair(false, *i + "压缩文件夹失败:" + e.what());
- }
-
-
- }
- else
- {
- int pos = i->find_last_of('\\');
- string filename(i->substr(pos + 1));
- ZRESULT zr = ZipAdd(newhz, filename.c_str(), (void *)i->c_str(), 0, ZIP_FILENAME);
- if (ZR_OK != zr)
- {
- result = make_pair(false, *i + "压缩失败");
- break;
- }
- }
- }
- CloseZip(newhz);
- if (false == result.first)
- {
- if (existFile(dstFile.c_str()))
- DeleteFileA(dstFile.c_str());
- return result;
- }
- return make_pair(true, "");
- }
- }
- #else
- #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
|