• bundle文件压缩库的使用


    目录

    认识bundle库

    压缩程序

    解压缩程序

    如何使用bundle库


    认识bundle库

    bundle是一个压缩库,可以直接嵌入到代码中直接进行使用支持23种压缩算法和2种存档格式,使用的时候只需要加入两个文件bundle.h和bundle.cpp即可。

    压缩和解压缩接口

    1. T pack( unsigned Q, T );//按照Q的方式对T中的数据进行解压缩
    2. T unpack( T ); //将T中的数据进行解压缩,unpack会根据压缩类型来进行解压缩
    3. //示例
    4. string s="hello world"
    5. //将s字符串的数据以LZIP的方式进行压缩,将压缩的结果返回给packed
    6. string packed=pack(bundle::LZIP,s);
    7. //将packed中的数据进行解压,并将解压的结果返回给unpacked
    8. string unpacked=unpack(packed);

    压缩的方式有如下这些:

    压缩程序

     实现一个压缩程序,对原始文件进行压缩,产生一个新的压缩文件。

    思路:

    • 打开原始文件,并将原始文件中的数据读取到body中
    • 用bundle库中的pack对body数据进行压缩,并将压缩结果返回给packed中
    • 在打开一个新文件,将packed中的数据写入到新文件中,形成压缩文件
    1. #include
    2. #include
    3. #include
    4. #include"bundle.h"
    5. using std::endl;
    6. using std::cout;
    7. int main(int argc,char* argv[]){
    8. if(argc<3){
    9. cout<<"package 原始文件路径名称 压缩包名"<
    10. return -1;
    11. }
    12. std::string ifilename=argv[1];
    13. std::string ofilename=argv[2];
    14. std::ifstream ifs;//定义一个ifstream变量
    15. //以二进制的方式将文件与ifstream对象关联起来
    16. ifs.open(ifilename,std::ios::binary);
    17. //获取文件大小
    18. ifs.seekg(0,std::ios::end);//跳转到文件末尾
    19. size_t fsize=ifs.tellg();//获取当前位置到文件开始的偏移量
    20. ifs.seekg(0,std::ios::beg);//在跳转到文件开始
    21. std::string body;
    22. body.resize(fsize);
    23. ifs.read(&body[0],fsize);
    24. //对body的数据进行压缩,压缩后将数据放进packed中
    25. //以LZIP格式压缩文件数据
    26. std::string packed=bundle::pack(bundle::LZIP,body);
    27. //打开一个新文件
    28. std::ofstream ofs;
    29. //以而二进制的方式将ofstream文件关联起来
    30. ofs.open(ofilename,std::ios::binary);
    31. //将压缩文件中的数据导入到ofstream流中
    32. ofs.write(&packed[0],packed.size());
    33. ifs.close();
    34. ofs.close();
    35. }

    编译生成packed文件,并将bundle.cpp文件压缩bundle.cpp.lz文件

    解压缩程序

     实现一个解压缩程序,对压缩文件进行解压,形成解压缩文件

    思路:

    • 将压缩文件中的数据读取到packed中
    • 利用unpack进行解压缩,并将解压缩的结果返回给body
    • 在打开一个新文件,将body数据写入到新文件中,形成解压缩文件
    1. #include
    2. #include
    3. #include
    4. #include"bundle.h"
    5. int main(int argc,char* argv[]){
    6. if(argc<3){
    7. return -1;
    8. }
    9. std::string ifilename=argv[1];//压缩文件名
    10. std::string ofilename=argv[2];//解压缩文件名
    11. std::ifstream ifs;
    12. ifs.open(ifilename,std::ios::binary);
    13. //获取文件大小
    14. ifs.seekg(0,std::ios::end);
    15. size_t fsize=ifs.tellg();//获取压缩文件大小
    16. //重新定位到文件的第一个字符
    17. ifs.seekg(0,std::ios::beg);
    18. std::string body;
    19. body.resize(fsize);
    20. //将文件数据读取到body中
    21. ifs.read(&body[0],fsize);
    22. //进行压缩
    23. std::string unpacked=bundle::unpack(body);
    24. std::ofstream ofs;
    25. ofs.open(ofilename,std::ios::binary);
    26. //将压缩数据压缩到压缩文件中
    27. ofs.write(&unpacked[0],unpacked.size());
    28. ofs.close();
    29. ifs.close();
    30. }

    运行结果:

    l利用md5sum检验bundle1.cpp和bundle.cpp文件的内容是否一样:

     md5sum利用某种特定的算法对文件内容进行了计算,如果文件内容的完全一致,则生成一个完全相同的字符串,如果文件内容不完全相同,则生成字符串的结果是不一致的。

    如何使用bundle库

    安装bundle库

    打开github页面

    点击r-lyeh-archived/bundle

    在浏览器上下载bundle库

     

     如果传输失败,那么很可能没有下载rz命令,所以需要yum install lrzsz进行下载。

    安装成功后

    2.用unzip 解压bundle-master.zip文件生成bundle-master文件

     

    3.将bundle-master库中的bundle.cpp和bundle.h文件到.cpp文件的当前路径下。

     4.在.cpp文件中包含bundle.h文件

    #include"bundle.h"

    5.编译的时候需要跟bunle.cpp文件一起编译,同时需要链接pthread库,如下:

    g++ unpacked.cpp bundle.cpp -o unpacked -lpthread

     

  • 相关阅读:
    EGOI2021 Lanterns / 灯笼
    JVM学习笔记——类加载和字节码技术篇
    协议栈——收发数据(拼接网络包,自动重发,滑动窗口机制)
    设置HTTP代理隧道
    1、Spring Security概述、快速入门
    wpf主页面解析
    勒索病毒最新变种.halo勒索病毒来袭,如何恢复受感染的数据?
    HttpServlet源码分析及HttpServletRequest接口
    【Shell脚本入门】
    前端无需install快速调试npm包,Console-Import使用
  • 原文地址:https://blog.csdn.net/sjp11/article/details/127832805