【云备份|| 日志 day2】FileUtil && JsonUtil

简介: 【云备份|| 日志 day2】FileUtil && JsonUtil

43c682a451bf49ed8d9c585264311f8c.png

util工具

fileUtil(文件操作类)

在客户端,又或者是在服务端,本质是都是对文件的读写和管理,所以有必要封装一个文件操作类。

class FileUtil{
private:
   std::string _name;
public:
  FileUtil(const std::string &name);
  size_t FileSize();// 文件大小
  time_t LastATime(); // 最后一次查看文件时间
  time_t LastMTime(); // 最后一次修改文件的时间
  std::string FileName();   //文件名字
  bool GetPosLen(std::string *content, size_t pos, size_t len);  //获取文件流pos 后len个长度的数据
  bool GetContent(std::string *content);  //获取文件内容
  bool SetContent(std::strint *content);   //写入文件
  bool Compress(const std::string &packname);   //压缩文件
  bool UnCompress(const std::string &filename);  //解压文件
  bool Exists();     //判断文件是否存在
  bool CreateDirectory();    //创建一个目录
  bool ScanDirectory(std::vector<std::string> *arry);  //查看目录下的文件内容
}

JsonUtil

服务端和客户端进行对话,传输数据,一定要跨网络传输,就一定需要将数据序列化(Serialize)和反序列化(UnSerialize)。

class JsonUtil{
public:
  static bool Serialize(const Json::Value &root, std::string *str);  //序列化
  static bool UnSerialize(const std::string &str, Json::Value *root);   //反序列化
};

注意:

编译时,我们需要链接第三方库

-lpthread -lstdc++fs -ljsoncpp -lbundle

-lpthread ,-lbundle: bundle.h中调用了线程库,所以需要链接。

-lstdc++fs : 文件中关于目录部分的函数编写调用了filesystem中的函数。

-ljsoncpp :序列和反序列化时需要。

代码:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <experimental/filesystem>
#include <sys/stat.h>
#include <jsoncpp/json/json.h>
#include "bundle.h"
namespace cloud{
  namespace fs = std::experimental::filesystem;
  class FileUtil{
    private:
      std::string _filename;
    public:
      FileUtil(const std::string &filename):_filename(filename){}
      bool Remove(){
        if (this->Exists()== false) {
          return true;
        }
        remove(_filename.c_str());
        return true;
      }
      int64_t FileSize(){
        struct stat st;
        if (stat(_filename.c_str(), &st) < 0) {
          std::cout << "get file size failed!\n";
          return -1;
        }
        return st.st_size;
      }
      time_t LastMTime(){
        struct stat st;
        if (stat(_filename.c_str(), &st) < 0) {
          std::cout << "get file size failed!\n";
          return -1;
        }
        return st.st_mtime;
      }
      time_t LastATime() {
        struct stat st;
        if (stat(_filename.c_str(), &st) < 0) {
          std::cout << "get file size failed!\n";
          return -1;
        }
        return st.st_atime;
      }
      std::string FileName(){
        // ./abc/test.txt
        size_t pos = _filename.find_last_of("/");
        if (pos == std::string::npos) {
          return _filename;
        }
        return _filename.substr(pos+1);
      }
      bool GetPosLen(std::string *body, size_t pos, size_t len){
        size_t fsize = this->FileSize();
        if (pos + len > fsize){
          std::cout << "get file len is error\n";
          return false;
        }
        std::ifstream ifs;
        ifs.open(_filename, std::ios::binary);
        if (ifs.is_open() == false) {
          std::cout << "read open file failed!\n";
          return false;
        }
        ifs.seekg(pos, std::ios::beg);
        body->resize(len);
        ifs.read(&(*body)[0], len);
        if (ifs.good() == false) {
          std::cout << "get file content failed\n";
          ifs.close();
          return false;
        }
        ifs.close();
        return true;
      }
      bool GetContent(std::string *body) {
        size_t fsize = this->FileSize();
        return GetPosLen(body, 0, fsize);
      }
      bool SetContent(const std::string &body) {
        std::ofstream ofs;
        ofs.open(_filename, std::ios::binary);
        if (ofs.is_open() == false) {
          std::cout << "write open file failed!\n";
          return false;
        }
        ofs.write(&body[0], body.size());
        if (ofs.good() == false) {
          std::cout << "write file content failed!\n";
          ofs.close();
          return false;
        }
        ofs.close();
        return true;
      }
      bool Compress(const std::string &packname){
        //1. 获取源文件数据
        std::string body;
        if (this->GetContent(&body) == false){
          std::cout << "compress get file content failed!\n";
          return false;
        }
        //2. 对数据进行压缩
        std::string packed = bundle::pack(bundle::LZIP, body);
        //3. 将压缩的数据存储到压缩包文件中
        FileUtil fu(packname);
        if (fu.SetContent(packed) == false){
          std::cout << "compress write packed data failed!\n";
          return false;
        }
        return true;
      }
      bool UnCompress(const std::string &filename) {
        //将当前压缩包数据读取出来
        std::string body;
        if (this->GetContent(&body) == false){
          std::cout << "uncompress get file content failed!\n";
          return false;
        }
        //对压缩的数据进行解压缩
        std::string unpacked = bundle::unpack(body);
        //将解压缩的数据写入到新文件
        FileUtil fu(filename);
        if (fu.SetContent(unpacked) == false){
          std::cout << "uncompress write packed data failed!\n";
          return false;
        }
        return true;
      }
      bool Exists() {
        return fs::exists(_filename);
      }
      bool CreateDirectory() {
        if (this->Exists()) return true;
        return fs::create_directories(_filename);
      }
      bool ScanDirectory(std::vector<std::string> *arry) {
        for(auto& p: fs::directory_iterator(_filename)) {
          if (fs::is_directory(p) == true){
            continue;
          }
          //relative_path 带有路径的文件名
          arry->push_back(fs::path(p).relative_path().string());
        }
        return true;
      }
  };
  class JsonUtil{
    public:
      static bool Serialize(const Json::Value &root, std::string *str){
        Json::StreamWriterBuilder swb;
        std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
        std::stringstream ss; 
        if (sw->write(root, &ss) != 0) {
          std::cout << "json write failed!\n";
          return false;
        }
        *str = ss.str();
        return true;
      }
      static bool UnSerialize(const std::string &str, Json::Value *root){
        Json::CharReaderBuilder crb;
        std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
        std::string err;
        bool ret = cr->parse(str.c_str(), str.c_str() + str.size(), root, &err);
        if (ret == false) {
          std::cout << "parse error: " << err << std::endl;
          return false; 
        }
        return true;
      }
  };
}


相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
【云备份|| 日志 day6】文件业务处理模块
【云备份|| 日志 day6】文件业务处理模块
269 0
|
存储 JSON 数据管理
【云备份|| 日志 day4】文件信息管理模块
【云备份|| 日志 day4】文件信息管理模块
289 0
|
JSON 数据格式
【云备份|| 日志 day3】服务端配置信息模块
【云备份|| 日志 day3】服务端配置信息模块
266 0
【云备份|| 日志 day3】服务端配置信息模块
|
存储 文件存储
【云备份|| 日志 day5】文件热点管理模块
【云备份|| 日志 day5】文件热点管理模块
244 0
|
存储 JSON 数据管理
【云备份|| 日志 day1】项目认识 && 环境准备
【云备份|| 日志 day1】项目认识 && 环境准备
194 0
|
XML 安全 Java
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板
本文介绍了Java日志框架的基本概念和使用方法,重点讨论了SLF4J、Log4j、Logback和Log4j2之间的关系及其性能对比。SLF4J作为一个日志抽象层,允许开发者使用统一的日志接口,而Log4j、Logback和Log4j2则是具体的日志实现框架。Log4j2在性能上优于Logback,推荐在新项目中使用。文章还详细说明了如何在Spring Boot项目中配置Log4j2和Logback,以及如何使用Lombok简化日志记录。最后,提供了一些日志配置的最佳实践,包括滚动日志、统一日志格式和提高日志性能的方法。
5289 32
【日志框架整合】Slf4j、Log4j、Log4j2、Logback配置模板
|
监控 安全 Apache
什么是Apache日志?为什么Apache日志分析很重要?
Apache是全球广泛使用的Web服务器软件,支持超过30%的活跃网站。它通过接收和处理HTTP请求,与后端服务器通信,返回响应并记录日志,确保网页请求的快速准确处理。Apache日志分为访问日志和错误日志,对提升用户体验、保障安全及优化性能至关重要。EventLog Analyzer等工具可有效管理和分析这些日志,增强Web服务的安全性和可靠性。
741 9
|
监控 容灾 算法
阿里云 SLS 多云日志接入最佳实践:链路、成本与高可用性优化
本文探讨了如何高效、经济且可靠地将海外应用与基础设施日志统一采集至阿里云日志服务(SLS),解决全球化业务扩展中的关键挑战。重点介绍了高性能日志采集Agent(iLogtail/LoongCollector)在海外场景的应用,推荐使用LoongCollector以获得更优的稳定性和网络容错能力。同时分析了多种网络接入方案,包括公网直连、全球加速优化、阿里云内网及专线/CEN/VPN接入等,并提供了成本优化策略和多目标发送配置指导,帮助企业构建稳定、低成本、高可用的全球日志系统。
1596 55