Snappy使用
Snappy是谷歌开源的压缩和解压的开发包,目标在于实现高速的压缩而不是最大的压缩
项目地址:GitHub - google/snappy:快速压缩器/解压缩器
Cmake版本升级
该项目需要比较新的cmake,CMake 3.16.3 or higher is required,所以回出现如下错误,更新cmake就可以
CMake Error at third_party/benchmark/CMakeLists.txt:1 (cmake_minimum_required):
CMake 3.16.3 or higher is required. You are running version 3.10.2
查看当前版本
cmake -version
删除安装的Cmake(最好直接删除路径下的Cmake,免得卸载Cmake出现多个库的问题)
搜索cmake路径
which cmake
切换到cmake路径
cd /usr/bin/
删除cmake
sudo rm cmake
下载高版本cmake:Index of /files/v3.18 (cmake.org)
解压安装包
tar xf cmake-3.18.3.tar.gz
安装cmake
cd cmake-3.18.3
sudo apt-get install libssl-dev build-essential
./configure
make
sudo make install
将编译的cmake复制到原始删除cmake的路径下
sudo cp bin/Cmake /usr/bin/
Snappy编译
1、克隆代码库
git clone https://github.com/google/snappy.git
2、编译代码库
git submodule update --init
mkdir build
cd build && cmake ../ && make
调用
C++ 程序使用Snappy,需要包含对应的链接库,
请包含来自 您的调用文件,并链接到已编译的库、头文件,最简单的方法
压缩:snappy::Compress(input.data(), input.size(), &output);
解压:snappy::Uncompress(input.data(), input.size(), &output);
示例,将Snappy编译好的libsnappy.a,snappy.h,snappy-stubs-public.h三个文件放在同一路径下
新建test.cpp测试程序,如下
#include <iostream>
#include "snappy.h"
#include <string.h>
using namespace std;
int main()
{
string input = "this is the demo for Snappy\n";
//repeat three times
for(int i=0; i<3; i++)
{
input += input;
}
cout << "origin:" << input.size() << endl;
cout << input << endl;
cout << "#########################################\n\n" << endl;
string Compress_output = "";
snappy::Compress(input.data(), input.size(), &Compress_output);
cout << "compress:" << Compress_output.size() << endl;
cout << Compress_output << endl;
cout << "#########################################\n\n" << endl;
string UnCompress_output = "";
snappy::Uncompress(Compress_output.data(), Compress_output.size(), &UnCompress_output);
cout << "uncompress:" << UnCompress_output.size() << endl;
cout << UnCompress_output << endl;
return 1;
}
编译执行
g++ test.cpp libsnappy.a -std=gnu++11 -o test
./test
压缩前224个字节,压缩后42个字节