| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #include "MyZip.h"
- #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, "");
- }
- }
|