C++第三方库 【文件解压缩】— bundle

C++第三方库 【文件解压缩】— bundleC 第三方库 bundle bundle 文件

大家好,欢迎来到IT知识分享网。

认识bundle库

bundle是一个GitHub开源的插件压缩库,提供文件的压缩和解压方法。可以直接嵌入到代码中,直接使用,支持23种压缩算法和2种存档格式,使用时只需加入bundle.h和bundle.cpp两个文件即可

压缩和解压接口

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

提供的压缩方式有如下:

 C++第三方库 【文件解压缩】— bundle

安装及使用

访问GitHub链接

C++第三方库 【文件解压缩】— bundle

在Linux下进入要存放bundle库的文件夹,使用如下指令:

git clone https://github.com/r-lyeh-archived/bundle.git

如果没有git,先安装git

sudo yum install 

如果下载失败,还可以通过先下载压缩包,再通过rz命令传输到Linux下,具体操作如下:

C++第三方库 【文件解压缩】— bundle

下载rz命令

sudo yum install lrzsz

C++第三方库 【文件解压缩】— bundle

再使用unzip解压

sudo yum install unzip //没有下载unzip的话

unzip bundle-master.zip bundle-master

 如果要使用bundle,需要将bundle.h和bundle.cpp放到项目文件夹下,编写.cpp文件时,包含头文件

#include “bundle.h”

 编译时,指明编译bundle.cpp,同时因为bundle.cpp中使用了多线程,还需要链接pthread库

g++ -o test test.cc bundle.cpp -std=c++11 -lptrhead

文件压缩

文件压缩的逻辑如下:

  1. 读取要压缩的文件的内容,原始文件数据存入字符串
  2. 使用bundle库的pack接口,选择压缩方式并压缩
  3. 将压缩后的内容写入新文件,形成压缩文件
#include <iostream> #include <fstream> #include <string> #include "bundle.h" //压缩 void zip(const std::string& ifilename, const std::string& ofilename) { //读取文件内容 std::ifstream ifs(ifilename.c_str(), std::ios::binary); ifs.seekg(0, std::ios::end);//跳转文件末尾 size_t fsize = ifs.tellg();//获取文件内容长度 ifs.seekg(0, std::ios::beg);//跳转文件起始 std::string body; body.resize(fsize); ifs.read(&body[0], fsize); //压缩 使用pack,指定压缩格式 std::string packed = bundle::pack(bundle::LZIP, body); //写入压缩文件 std::ofstream ofs(ofilename.c_str(), std::ios::binary); ofs.write(packed.c_str(), packed.size()); ifs.close(); ofs.close(); } int main(int argc, char* argv[]) { if(argc < 3) { std::cout << "argv[1]是原始文件路径\n"; std::cout << "argv[2]是压缩包名称\n"; return -1; } zip(argv[1], argv[2]); return 0; }

C++第三方库 【文件解压缩】— bundle

运行compress程序,指明要压缩的原始文件,和压缩后形成的压缩文件

可以看到压缩后形成bundle.cpp.lz的大小为668K,而原始文件bundle.cpp大小为5.4M

解压文件

文件解压逻辑如下:

  1. 读取压缩文件内容,文件数据存入字符串
  2. 使用bundle库的unpack接口,解压文件数据
  3. 将解压后的内容写入新文件
#include <iostream> #include <fstream> #include <string> #include "bundle.h" //解压 void unzip(const std::string& ifilename, const std::string& ofilename) { std::ifstream ifs(ifilename.c_str(), std::ios::binary); ifs.seekg(0, std::ios::end); size_t fsize = ifs.tellg(); ifs.seekg(0, std::ios::beg); std::string body; body.resize(fsize); ifs.read(&body[0], fsize); //解压 std::string unparked = bundle::unpack(body); //复原文件 std::ofstream ofs(ofilename.c_str(), std::ios::binary); ofs.write(unparked.c_str(), unparked.size()); ifs.close(); ofs.close(); } int main(int argc, char* argv[]) { if(argc < 3) { std::cout << "argv[1]是压缩文件名称\n"; std::cout << "argv[2]是新文件名称\n"; return -1; } unzip(argv[1], argv[2]); return 0; }

C++第三方库 【文件解压缩】— bundle

运行uncompress程序,解压bundle.cpp.lz文件,生成bundle2.cpp

可以看到二者文件大小一样,md5sum生成的md5散列值也相同,说明两个文件内容相同

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/125007.html

(0)
上一篇 2025-10-01 16:00
下一篇 2025-10-01 16:10

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信