006-snappy-谷歌高速压缩库

一、介绍

  • 速度优先:压缩和解压缩速度极快,远超传统zlib等压缩库;
  • 资源轻量:内存和cpu占用低,适合高性能系统;
  • 压缩率适中:压缩率通常比zlib低10~30%,但速度快2-10倍;
  • c++友好:内部实现和外部接口均以c++为主;
  • 跨平台

二、主要接口

1.压缩数据

size_t Compress(char* input, size_t input_length, std::string* output);

2.解压缩数据

bool Uncompress(char* input, size_t input_length, &output);

3.计算压缩后最大可能长度

MaxCompressedLength(input_length);

4.获取原始数据长度

GetUncompressedLength(input, input_length, & result);

三、示例

#include <cstdlib>
#include <fstream>
#include <iostream>

#include <snappy.h>

// 测试压缩字符串,并将压缩结果写入指定的文件
void testCompress(std::string const& ori_data, std::string const& file_name)
{
    size_t max_compress_size = snappy::MaxCompressedLength(ori_data.size());

    std::string compressed_data(max_compress_size, '\0');

    size_t compressed_size = snappy::Compress(ori_data.data(), ori_data.size(), &compressed_data);

    std::cout << "Original size: " << ori_data.size()
              << "\nCompressed size: " << compressed_size
              << "\nCompression ratio: " << static_cast<double>(ori_data.size()) / compressed_size
              << std::endl;

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

    ofs.write(compressed_data.data(), compressed_data.size());

    ofs.close();
}

std::string testDecompress(std::string const& file_name)
{
    std::ifstream ifs(file_name, std::ios::binary);
    if (!ifs.is_open()) {
        {
            return "";
        }
    }

    auto begin = std::istreambuf_iterator<std::ifstream::char_type>(ifs);
    auto end = std::istreambuf_iterator<std::ifstream::char_type>();

    std::string compressed_data(begin, end);

    ifs.close();

    std::string decompressed_data;
    bool ok = snappy::Uncompress(compressed_data.data(), compressed_data.size(), &decompressed_data);

    if (!ok) {
        std::cerr << "Failed to decompress data" << std::endl;
        return "";
    }

    return decompressed_data;
}

int main()
{
    std::system("chcp 65001 > nul");

    std::cout << "压缩=====================================\n";

    std::string ori_data =
        R"([cmake] Not searching for unused variables given on the command line.
[cmake] -- The CXX compiler identification is GNU 15.1.0
[cmake] -- The C compiler identification is GNU 15.1.0
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: D:/Environment/MSYS2/ucrt64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: D:/Environment/MSYS2/ucrt64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Configuring done (3.7s)
[cmake] -- Generating done (0.0s)
[cmake] -- Build files have been written to: C:/Users/WDJ/Desktop/code/snappy/build/Debug
)";

    std::string file_name = "compressed.snappy";

    testCompress(ori_data, file_name);

    std::cout << "解压=====================================\n";

    std::string decompressed_data = testDecompress(file_name);

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

    std::system("pause > nul");
    return 0;
}

运行结果:

压缩=====================================
Original size: 885
Compressed size: 423
Compression ratio: 2.0922
解压=====================================
Decompressed data: [cmake] Not searching for unused variables given on the command line.
[cmake] -- The CXX compiler identification is GNU 15.1.0
[cmake] -- The C compiler identification is GNU 15.1.0
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: D:/Environment/MSYS2/ucrt64/bin/g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Check for working C compiler: D:/Environment/MSYS2/ucrt64/bin/gcc.exe - skipped
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Configuring done (3.7s)
[cmake] -- Generating done (0.0s)
[cmake] -- Build files have been written to: C:/Users/WDJ/Desktop/code/snappy/build/Debug