当前位置: 首页 > 工具软件 > snappy-start > 使用案例 >

ubuntu下的snappy安装配置以及测试

融修平
2023-12-01

snappy是google的一款压缩/解压库。github
环境:
ubuntu16.04
gcc version 5.4.0(系统自带)
cmake 3.5.1
snappy 1.1.8

snappy下载

下载地址选择1.1.8版本下载

cmake配置

sudo apt-get install cmake

snappy安装

首先将下载好的文件(snappy-1.1.8.tar.gz)解压,进入解压好的文件夹打开终端,输入如下命令:

mkdir build
cd build
cmake ../
make

此时在build文件夹下可以得到静态链接库libsnappy.a以及头文件snappy-stubs-public.h

编写代码

在snappy.h提供了两个函数调用:

#压缩
size_t Compress(const char* input, size_t input_length, std::string* compressed);
#解压
bool Uncompress(const char* compressed, size_t compressed_length, std::string* uncompressed);

可以看出snappy是根据字符来进行压缩的。

按行压缩文件

按行读取文件并进行压缩,并将压缩后的值写入文件中。

#smain.cpp

#include <iostream>
#include "snappy.h"
#include <string.h>
#include <fstream>

using namespace std;

int main(int argc, char* argv[]) {
	ifstream ifile(argv[1]);
	ofstream ofile(argv[2]);
	string instr;
	string outstr;

	while(ifile >> instr) {
		//cout << instr << endl;
		snappy::Compress(instr.data(), instr.size(), &outstr);
		ofile << outstr;
		//cout << outstr << endl;
	}
	return 0;
}

将snappy编译后的静态链接库libsnappy.a以及头文件snappy.hsnappy-stubs-public.h放到与上面代码相同的文件目录下,然后进行编译命令:

g++ smain.cpp libsnappy.a -std=gnu++11

得到可执行文件a.out,然后执行如下命令进行压缩:

./a.out inputfile outputfile

其中inputfile是带压缩文件路径,outputfile是压缩后的文件路径。
注意:按行压缩效果很不理想

按字符读取文件并进行压缩

按字节读取文件并进行压缩,并将压缩后的值写入文件中。
由于机器原因,本次实验采用的是每一兆字节压缩一次。并统计压缩解压时间。
如下是压缩代码

#smain2.cpp
#include <iostream>
#include "snappy.h"
#include <string.h>
#include <fstream>
#include <ctime>

using namespace std;
clock_t startt,endt;

int main(int argc, char* argv[]) {
	startt=clock();	
	ifstream ifile(argv[1]);
	ofstream ofile1(argv[2]);
	ofstream ofile2(argv[3]);
	string outstr;
	char buf[1048576];
	

	for(;;) {
		ifile.read(buf, 1048576);
		snappy::Compress(buf, ifile.gcount(), &outstr);
		ofile1 << outstr;
		ofile2 << outstr.size() << endl;
		if(ifile.eof()){
			break;
		}
	}
	endt=clock();
	double endtime=(double)(endt-startt)/CLOCKS_PER_SEC;
	cout<<"Total time:"<<endtime<<endl;
		
	return 0;
}

因为我们是根据字节进行压缩,压缩后的字节也需要记录,这样解压的时候就可以根据记录来进行解压。需要主函数传入三个参数:argv[1]记录要压缩的文件路径、argv[2]记录压缩文件路径、argv[3]记录压缩后的字节大小文件路径。
编译命令:

g++ smain2.cpp libsnappy.a -std=gnu++11 -o smain2.out

运行命令:

./smain2.out inputfile1 outputfile1 outputfile2

如下是解压代码:

#smain3.cpp
#include <iostream>
#include "snappy.h"
#include <string.h>
#include <fstream>
#include <ctime>

using namespace std;
clock_t startt,endt;

int main(int argc, char* argv[]) {
	startt=clock();
	ifstream ifile(argv[1]);
	ifstream ifile1(argv[2]);
	ofstream ofile2(argv[3]);
	string outstr = "";
	int size;
	char buf[1048576];

	for(;;) {
		ifile1 >> size;
		ifile.read(buf, size);
		snappy::Uncompress(buf, size, &outstr);
		if(ifile.eof()){
			break;
		}
		ofile2 << outstr;
	}
	endt=clock();
	double endtime=(double)(endt-startt)/CLOCKS_PER_SEC;
	cout<<"Total time:"<<endtime<<endl;
		
	return 0;
}

其中主函数也是传递三个参数:argv[1]记录压缩文件路径、argv[2]记录压缩字节大小文件路径、argv[3]记录解压后的文件路径。
编译命令:

g++ smain3.cpp libsnappy.a -std=gnu++11 -o smain3.out

运行命令:

./smain3.out inputfile1 inputfile2 outputfile1
 类似资料: