#include <boost\filesystem.hpp> #include <KHAboutLocalEnvirTool.h> #include <string> #include " XZip.h "//Copy folder implementation void Copy_all_file (LPCSTR file_path, std_string &backup_path)//The first parameter is the copied file, the second one is the address {if (! Boost::filesystem::exists (Backup_path.c_str ())))//Determine if the directory of the backup file exists and does not exist create {boost::filesystem::create_directory ( Backup_path.c_str ());//directory does not exist, create} std::vector< std_string > file_list; std_string path (file_path);p Ath. Append ("\ \"); File_list (File_path, file_list);//Get a list of files, implement for by FindFirstFile (Auto it = File_list.begin (); it! = File_ List.end (); it++) {std_string file_name = (*it);//file path std_string file_path_str;file_path_str. Assign (File_name.c_str ()); file_name. Replace (Path.c_str (), "");//Database file name 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 ()); Boost::filesystem::exists (copyfile_dir.c_STR ()))) {boost::filesystem::p ath Dir_pa (copyfile_dir.c_str ());d Ir_pa = Dir_pa.parent_path (); if (!boost::filesystem :: Exists (DIR_PA)) {boost::filesystem::create_directory (DIR_PA);//The parent directory does not exist to create}boost::filesystem::create_directory ( Copyfile_dir.c_str ());//directory does not exist create}copyfilea (File_path_str.c_str (), Copyfile_path.c_str (), TRUE);}} ZIP file compression implementation 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_li St.end (); it++) {std_string file_name = (*it);//file path file_name. Replace (Path_str.c_str (), "");//database file name, PTR is its parent path 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 (' * ');//Find the first file/directory, get a Find handle hlistfile = Findfirstfilea (str_file_ Path.c_str (), &findfiledata);//judgment handle 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;//determine file attributes, directory 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 ());} The reader can add self-judging file attributes according to the contents of the file attribute table. } while (Findnextfilea (Hlistfile, &findfiledata));} FindClose (hlistfile); return;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
VC Replication Folder implementation (can replicate three-tier path) and zip compression implementation