bundle库的使用

  1 #include<iostream>                                                                            
  2 #include<string> 
  3 #include<fstream>
  4 #include"bundle.h"
  5 using namespace std;
  6 bool Read(const string &name,string *body)
  7 {                                                           
  8   ifstream ifs;//文件指针
  9   ifs.open(name,std::ios::binary);//第一个参数为要打开文件名,第二个参数为要使用什么方式打开,    本次使用的方式为以二进制的方式打开。
 10   if(ifs.is_open() == false)//是用来查看是否打开文件成功的接口. 11   {               
 12     cout<<"read open false"<<endl;                       
 13     return false;
 14   }                                                           
 15   ifs.seekg(0,std::ios::end);//表示从末尾位置开始偏移,偏移0个大小
 16   size_t fsize = ifs.tellg();//获取当前位置相对于文件起始位置的偏移量。(这也是为什么有上一步>    的原因,找到文件的大小)                                 
 17   ifs.seekg(0,std::ios::beg);//表示从起始位置开始偏移,偏移0个大小 18   body->resize(fsize);
 19   ifs.read(&(*body)[0],fsize);//由于string.c_str()的返回值是一个const char*的类型的,但是第一>    个参数我们要的是要读入文件的起始位置,所以用一步取地址的方法得到。
 20   if(ifs.good() == false)//用来查看文件读取数据是否成功.
 21   {
 22     cout<<"read read fasle"<<endl;
 23     return false;
 24   }
 25   ifs.close();
 26   return true;
 27 }
 28 bool Write(const string &name,const string &body)
 29 {
 30   ofstream ofs;//文件指针.
 31   ofs.open(name,std::ios::binary);
 32   if(ofs.is_open() == false)                                                                  
 33   {
 34     cout<<"write open false"<<endl;
 35     return false;
 36   }
 37   ofs.write(body.c_str(),body.size());
 38   if(ofs.good() == false)
 39   {
 40     cout<<"write write false"<<endl;
 41     return false;
 42   }
 43   ofs.close();
 44   return true;
 45 }
 46 void compress(const string &filename,const string &packname)
 47 {
 48   string body;
 49   Read(filename,&body);//将文件中的内容放入body中。
 50   string packed = bundle::pack(bundle::LZIP,body);//这个函数第一个参数是要压缩的类型,第二个参    数是要压缩的文件。
 51   Write(packname,packed);//将压缩后的内容放入在packname中                                     
 52 }
 53 void uncompress(const string &filename,const string &packname)
 54 {
 55   string packed;
 56   Read(packname,&packed);//从压缩包中将数据读取到packed中
  57   string body = bundle::unpack(packed);//对要压缩的数据进行解压缩,并将解压缩后的数据放在body>    中。
 58   Write(filename,body);//从body中将数据放在新文件中。
 59 }
 60 
 61 int main()
 62 {
 63   compress("./lrttest.cpp","./lrttest.zip");
 64   uncompress("./lrttest.txt","./lrttest.zip");
 65   return 0;
 66 }              

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆皮骷髏人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值