c++ - 使用 GZIP 压缩的 Protobuf

标签 c++ protocol-buffers

我在尝试使用 protobufunsigned char 数组序列化到文件中时遇到问题.

我认为问题可能与我的某些语法或 API 的误用有关。 我也尝试过 std::fstream。 仅供引用,Windows 8.1 和 VS2013 是构建环境。

场景原型(prototype)

syntax = "proto3";

package Recipe;

message Scene
{
    repeated int32 imageData = 1 [packed=true];
}

源代码.cpp

#include <iostream>
#include <fstream>
#include <ostream>
#include <istream>
#include <string>
#include <cstdint>
#include "Scene.pb.h"
#include <google\protobuf\io\zero_copy_stream_impl.h>
#include <google\protobuf\io\gzip_stream.h>

int const _MIN = 0;
int const _MAX = 255;
unsigned int const _SIZE = 65200000;
unsigned int const _COMPRESSION_LEVEL = 10;

void randWithinUnsignedCharSize(uint8_t * buffer, unsigned int size)
{
    for (size_t i = 0; i < size; ++i)
    {
        buffer[i] = _MIN + (rand() % static_cast<int>(_MAX - _MIN + 1));
    }
}

using namespace google::protobuf::io;

int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    Recipe::Scene * scene = new Recipe::Scene();
    uint8_t * imageData = new uint8_t[_SIZE];
    randWithinUnsignedCharSize(imageData, _SIZE);

    for (size_t i = 0; i < _SIZE; i++)
    {
        scene->add_imagedata(imageData[i]);
    }
    std::cout << "scene->imagedata_size() " << scene->imagedata_size() << std::endl;
    {
        std::ofstream output("scene.art", std::ofstream::out | std::ofstream::trunc | std::ofstream::binary);
        OstreamOutputStream outputFileStream(&output);
        GzipOutputStream::Options options;
        options.format = GzipOutputStream::GZIP;
        options.compression_level = _COMPRESSION_LEVEL;

        GzipOutputStream gzipOutputStream(&outputFileStream, options);

        if (!scene->SerializeToZeroCopyStream(&gzipOutputStream)) {
            std::cerr << "Failed to write scene." << std::endl;
            return -1;
        }
    }
    Recipe::Scene * scene1 = new Recipe::Scene();
    {
        std::ifstream input("scene.art", std::ifstream::in | std::ifstream::binary);
        IstreamInputStream inputFileStream(&input);
        GzipInputStream gzipInputStream(&inputFileStream);

        if (!scene1->ParseFromZeroCopyStream(&gzipInputStream)) {
            std::cerr << "Failed to parse scene." << std::endl;
            return -1;
        }
    }
    std::cout << "scene1->imagedata_size() " << scene1->imagedata_size() <<std::endl;
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}

最佳答案

您的代码中似乎有错字。压缩级别根​​据 documentation在 0-9 范围内。您错误地将压缩级别设置为 10。

当更正为以下内容时,您的示例对我有用:

unsigned int const _COMPRESSION_LEVEL = 9;

关于c++ - 使用 GZIP 压缩的 Protobuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951512/

相关文章:

qt - 使用 qmake 链接 .proto 文件

java - 如何在 Cloudera hadoop 中使用外部 jar?

c++ - 在 C++ 中,我想为包含个人定义类的双端队列实现一个环形迭代器

c++ - 为什么在 vector 循环中使用 '!="比使用 '<' 更好? (C++)

c++ - Google Test中的测试类的构造方法

c++ - 如何在 linux 上通过 cmake 链接 google protobuf 库?

c++ - 在哪里提取 Linux Mint 19 下 wxWidgets 的源代码(.cpp 文件)以进行内部调试?

c++ - OpenMP 嵌套,不等号。迭代次数

c++ - 由于 PROTOBUF 服务器中的字符串数据类型变量和客户端通过 cpp 中 recv 端的套接字进行通信而导致段错误

c# - 如何使用 protobuf 将 ServiceStack 服务与非 ServiceStack 客户端集成?