做网站软件图标是一个箭头的,沈阳网站建设哪家做得好,东莞网站快速排名优化,遂宁网站建设公司文章目录 1. 项目文件操作工具类设计 1. 项目文件操作工具类设计
根据前面的分析#xff0c;这个文件类的基本属性如下#xff1a;
文件大小信息文件最后修改时间文件最后一次访问时间#xff0c;方便文件的热点管理文件名称#xff0c;需要从http 请求行上的uri中获取基… 文章目录 1. 项目文件操作工具类设计 1. 项目文件操作工具类设计
根据前面的分析这个文件类的基本属性如下
文件大小信息文件最后修改时间文件最后一次访问时间方便文件的热点管理文件名称需要从http 请求行上的uri中获取基础文件读写接口 写数据为SetContent读数据GetContent获取文件指定位置指定长度的数据使其支持断点续传获取文件夹的所有文件名称判断文件是否存在方法创建目录方法名称是获取的文件名称文件压缩解压缩方法
错误日志函数log.hpp
// 项目错误日志打印
#pragma once
#include iostream
#include stdio.h
#include string
#include time.h
#define INFO 1
#define WARNING 2
#define ERROR 3
#define FATAL 4
#define LOG(level, message) Log(#level, message, __FILE__, __LINE__) // #将宏参数转化为字符串// 时间戳转化为时间信息
static std::string convertTimeStamp2TimeStr(time_t timeStamp)
{struct tm *timeinfo nullptr;char buffer[80];timeinfo localtime(timeStamp);strftime(buffer, 80, %Y-%m-%d %H:%M:%S, timeinfo);// printf(%s\n, buffer);return std::string(buffer);
}// 日志级别日志信息时间戳错误文件名称错误行数
// 日志级别 INFO,WARNING,ERROR,FATAL
void Log(std::string level, std::string msg, std::string file_name, int line)
{std::cout [ level ] [ convertTimeStamp2TimeStr(time(nullptr)) ] [ msg ] [ file_name ] [ line ] std::endl;
}文件工具类目录操作方法补充
c库函数scandir函数这个函数比较复杂使用这个也可以这里使用C17Filesystem类中的方法 Filesystem library (since C17) 注意使用这个库需要在编译时加上-lstdcfs
项目文件操作工具类设计
#pragma once
#include iostream
#include fstream
#include string
#include vector
#include sys/stat.h
#include log.hpp
#include ../bundle/bundle.h
// #include filesystem
#include experimental/filesystem
namespace CloudBackups
{namespace fs std::experimental::filesystem;class FileUtil{private:std::string _filepath; // 文件名称 uri格式struct stat st; // 文件属性public:FileUtil(const std::string filepath){_filepath filepath;if (stat(_filepath.c_str(), st) 0){LOG(WARNING, get file stat failed! maybe this file not exits);}}int64_t filesize() { return st.st_size; } // 获取文件大小失败返回-1time_t last_modify_time() { return st.st_mtime; } // 获取文件最后修改时间time_t last_visit_time() { return st.st_atime; } // 获取文件最后访问时间std::string filename() // 文件名称{size_t pos _filepath.find_last_of(/);if (pos std::string::npos){return _filepath;}return _filepath.substr(pos 1);}bool getPoslen(std::string body, size_t pos, size_t len) // 从文件中读取len个字节从pos位置开始读取读取内容放到body中为了实现断点续传{size_t size this-filesize(); // 文件大小if (pos size){LOG(ERROR, pos is out of range!);return false;}if (pos len size){LOG(ERROR, pos len is out of range!);return false;}std::ifstream ifs;ifs.open(_filepath.c_str(), std::ios::binary);if (!ifs.is_open()){LOG(ERROR, open file failed!);return false;}ifs.seekg(pos, std::ios::beg);body.resize(len);ifs.read(body[0], len);if (!ifs.good()){// 上次读取出错LOG(ERROR, read file failed!);ifs.close();return false;}ifs.close();return true;}bool getContent(std::string body) // 获取整体的文件数据{size_t size this-filesize();return getPoslen(body, 0, size);}bool setContent(const std::string body) // 设置文件内容{std::ofstream ofs;ofs.open(_filepath.c_str(), std::ios::binary);if (!ofs.is_open()){LOG(ERROR, open file failed!);return false;}ofs.write(body.c_str(), body.size());if (!ofs.good()){// 上次写入出错LOG(ERROR, write file failed!);ofs.close();return false;}ofs.close();return true;}bool zip(const std::string packname) // 文件压缩功能,传入压缩后名称{// 读取源文件所有内容std::string body;if (this-getContent(body) false){// 获取源文件数据失败LOG(ERROR, get file content failed!);return false;}// 对数据进行压缩std::string packed bundle::pack(bundle::LZIP, body);// 保存压缩后的数据FileUtil file(packname);if (file.setContent(packed) false){ // 保存压缩后的数据失败LOG(ERROR, save zip file content failed!);return false;}return true;}bool unzip(const std::string filename) // 文件解压缩功能,传入解压缩文件名称{// 读取当前压缩的数据std::string body;if (this-getContent(body) false){// 获取源文件数据失败LOG(ERROR, get zip file content failed!);return false;}// 对压缩的数据进行解压std::string unpacked bundle::unpack(body);// 保存解压数据FileUtil file(filename);if (file.setContent(unpacked) false){ // 保存解压数据失败LOG(ERROR, save unzip file content failed!);return false;}return true;}bool isExit() { return fs::exists(_filepath); } // 判断文件是否存在bool mkdir() // 创建文件夹{if (this-isExit()){return true;}return fs::create_directories(_filepath);}bool ls(std::vectorstd::string files) // 扫描文件夹,并返回里面的文件{for (auto pos : fs::directory_iterator(_filepath)){if (fs::is_directory(pos) true){continue; // 目录不出来}files.push_back(fs::path(pos).relative_path().string()); // 获取文件的相对路径}return true;}};
}根据上图可知项目文件属性获取文件读写文件压缩与解压缩基本功能没有问题
代码位置 Gitee地址 Gitee地址