压缩归档库-Snappy介绍

236 阅读2分钟

1.简介

Snappy 是一个 C++ 编写的压缩和解压缩库,由 Google 开发。它专为速度而设计,而不是最大压缩率或与其他压缩库的兼容性。 Snappy 通常用于需要快速压缩和解压缩的场景。 Snappy具有以下属性:

  • 快速:压缩速度达到250 MB/秒及以上。
  • 稳定:Snappy在谷歌的生产环境中压缩和解压缩了数PB的数据。Snappy比特流格式是稳定的,不会在不同版本之间发生变化。
  • 鲁棒性:Snappy解压缩器的设计不会在面对损坏或恶意输入时崩溃。
  • 免费开源软件:Snappy是在BSD类型的许可证下获得许可的。

2.环境搭建

下载地址:github.com/google/snap… 编译:下载完成后,解压缩,使用cmake编译。 在这里插入图片描述 这个时候会报错,googletest目录下找不到cmakelist文件。这个时候打开CMakeLists.txt修改,把以下两个地方注释掉,再重新编译生成库。 在这里插入图片描述 在这里插入图片描述 生成库如下图所示: 在这里插入图片描述 拷贝如下文件和lib文件到我们的demo工程中。 在这里插入图片描述 配置visual studio环境,请看Jsoncpp介绍。如何配置include、lib目录。

3.示例

压缩/解压缩数据。

#include <iostream>
#include <snappy.h>

int main()
{
	// 原始数据
	std::string input = "This is a string that we want to compress using Snappy.";

	// 压缩数据
	std::string compressed;
	snappy::Compress(input.data(), input.size(), &compressed);

	std::cout << "Compressed size: " << compressed.size() << std::endl;

	// 解压缩数据
	std::string decompressed;
	if (!snappy::Uncompress(compressed.data(), compressed.size(), &decompressed)) 
	{
		std::cerr << "Failed to decompress the data." << std::endl;
		return 1;
	}

	std::cout << "Decompressed data: " << decompressed << std::endl;

	return 0;
}

压缩/解压缩文件

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

// 压缩文件
bool compressFile(const std::string& input_filename, const std::string& output_filename) 
{
	std::ifstream input(input_filename, std::ios::binary);
	if (!input.is_open()) 
	{
		std::cerr << "Failed to open input file." << std::endl;
		return false;
	}

	std::string input_data((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
	input.close();

	std::string compressed;
	snappy::Compress(input_data.data(), input_data.size(), &compressed);

	std::ofstream output(output_filename, std::ios::binary);
	if (!output.is_open()) {
		std::cerr << "Failed to open output file." << std::endl;
		return false;
	}

	output.write(compressed.data(), compressed.size());
	output.close();

	return true;
}

// 解压缩文件
bool decompressFile(const std::string& input_filename, const std::string& output_filename) 
{
	std::ifstream input(input_filename, std::ios::binary);
	if (!input.is_open())
	{
		std::cerr << "Failed to open input file." << std::endl;
		return false;
	}

	std::string compressed((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
	input.close();

	std::string decompressed;
	if (!snappy::Uncompress(compressed.data(), compressed.size(), &decompressed)) 
	{
		std::cerr << "Failed to decompress the data." << std::endl;
		return false;
	}

	std::ofstream output(output_filename, std::ios::binary);
	if (!output.is_open())
	{
		std::cerr << "Failed to open output file." << std::endl;
		return false;
	}

	output.write(decompressed.data(), decompressed.size());
	output.close();

	return true;
}

int main() 
{
	const std::string input_file = "E:/2.mp4";
	const std::string compressed_file = "E:/2.snappy";
	const std::string decompressed_file = "E:/3.mp4";

	// 压缩文件
	if (compressFile(input_file, compressed_file)) {
		std::cout << "File compressed successfully." << std::endl;
	}
	else {
		std::cerr << "Failed to compress file." << std::endl;
		return 1;
	}

	// 解压缩文件
	if (decompressFile(compressed_file, decompressed_file)) {
		std::cout << "File decompressed successfully." << std::endl;
	}
	else {
		std::cerr << "Failed to decompress file." << std::endl;
		return 1;
	}

	return 0;
}