#include <boost\filesystem.hpp>
#include <KHAboutLocalEnvirTool.h>
#include <string>
#include "XZip.h"
//复制文件夹实现
void copy_all_file(LPCSTR file_path, std_string &backup_path) //第一个参数为被复制文件,第二个是复制到的地址
{
if(!(boost::filesystem::exists(backup_path.c_str())))//判断备份文件的目录是否存在,不存在则创建
{
boost::filesystem::create_directory(backup_path.c_str());//目录不存在,创建
}
std::vector< std_string > file_list ;
std_string path(file_path);
path.Append("\\");
file_list(file_path, file_list );//获取文件列表,通过findfirstfile实现
for ( auto it = file_list.begin(); it != file_list.end() ; it++ )
{
std_string file_name = (*it);//文件路径
std_string file_path_str;
file_path_str.Assign(file_name.c_str());
file_name.Replace(path.c_str(), "");//数据库文件名
std_string copyfile_path,copyfile_dir;
copyfile_dir.Append(backup_path.c_str());
copyfile_dir.Append(file_name.c_str());
copyfile_path.Assign(copyfile_dir.c_str());
copyfile_dir.erase(copyfile_dir.rfind("\\"), copyfile_dir.size());
if(!(boost::filesystem::exists(copyfile_dir.c_str())))
{
boost::filesystem::path dir_pa(copyfile_dir.c_str());
dir_pa = dir_pa.parent_path();
if (!boost::filesystem::exists(dir_pa))
{
boost::filesystem::create_directory(dir_pa);//父目录不存在就创建
}
boost::filesystem::create_directory(copyfile_dir.c_str());//目录不存在就创建
}
CopyFileA(file_path_str.c_str(),copyfile_path.c_str(), TRUE );
}
}
//zip文件压缩实现
HZIP hz_res_value = CreateZip(CA2T(file_path.c_str()), 0 , ZIP_FILENAME);
if ( hz_res_value )
{
std::vector< std_string > file_list ;
file_list(backup_dir_path.c_str(), file_list );
for ( auto it = file_list.begin(); it != file_list.end() ; it++ )
{
std_string file_name = (*it);//文件路径
file_name.Replace(path_str.c_str(), "");//数据库文件名,ptr为其父路径
ZRESULT zRet = ZipAdd( hz_res_value, CA2T(file_name.c_str()), CA2T((*it).c_str()), NULL, ZIP_FILENAME );
}
CloseZip( hz_res_value );
}
void file_list(LPCSTR szPath, std::vector< std_string > & flist )
{
if (szPath == NULL)
{
return;
}
WIN32_FIND_DATAA FindFileData;
HANDLE hListFile;
CHAR szFilePath[MAX_PATH] = { 0 };
std_string str_file_path(szPath);
if (!str_file_path.EndsWith("\\"))
{
str_file_path.push_back('\\');
}
std::string new_dir = szPath;
str_file_path.push_back('*');
//查找第一个文件/目录,获得查找句柄
hListFile = FindFirstFileA(str_file_path.c_str(), &FindFileData);
//判断句柄
if (hListFile == INVALID_HANDLE_VALUE)
{
return;
}
else
{
do
{
if (strcmp(FindFileData.cFileName, ".") == 0 || strcmp(FindFileData.cFileName, "..") == 0)
{
continue;
}
std::string _new_dir = new_dir +"\\"+ FindFileData.cFileName;
//判断文件属性,目录
if (FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
{
file_list(_new_dir.c_str(), flist);
//RemoveDirectoryA(_new_dir.c_str());
}
else
{
flist.push_back(_new_dir.c_str());
}
//读者可根据文件属性表中的内容自行添加判断文件属性。
} while (FindNextFileA(hListFile, &FindFileData));
}
FindClose(hListFile);
return;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/qingzai_/article/details/46981507