#include "MyZip.h" #if (defined _WIN32 || defined _WIN64) #include #include #include #include "XZip.h" #include #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 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 ZipVector(const std::string &dstFile, const std::vector &srcArr) { if (existFile(dstFile.c_str())) DeleteFileA(dstFile.c_str()); for (vector::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 result = make_pair(true, ""); for (vector::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 #include #include #include "remoteBase.h" #include 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 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 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 ZipVector(const std::string& dstFile, const std::vector& 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