ad

带有GZIP紧缩的Protobuf-英雄云拓展知识分享

匿名投稿 418 2024-01-21

我在试图序列化一系列时有问题 unsigned charGZIP 使用 protobuf 与图书馆一起玩。

我认为问题可能与我的某些语法或滥用API有关。我也尝试了 std::fstream。仅供参考,Windows 8.1&VS2013是建筑环境。

场景

syntax = "proto3";

package Recipe;

message Scene

带有GZIP紧缩的Protobuf-英雄云拓展知识分享

{

repeated int32 imageData = 1 [packed=true];

}

source.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 ⑴;

}

}

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 ⑴;

}

}

std::cout << "scene1->imagedata_size() " << scene1->imagedata_size() <<std::endl;

google::protobuf::ShutdownProtobufLibrary();

return 0;

}

看答案

您仿佛在代码中有错字。紧缩水平是针对 文档 在0⑼范围内。您将毛病的紧缩水平设置为10。

您的示例在纠正至:

unsigned int const _COMPRESSION_LEVEL = 9;


🚀🌟 点击注册 免费试用超级应用平台-英雄云企业级hpapaas 🌟🚀 😃👉🌐

免责声明:

本网址(www.yingxiongyun.com)发布的材料主要源于独立创作和网友匿名投稿。此处提供的所有信息仅供参考之用。我们致力于提供准确且可信的信息,但不对材料的完整性或真实性作出任何保证。用户应自行验证相关信息的正确性,并对其决策承担全部责任。对于由于信息的错误、不准确或遗漏所造成的任何损失,本网址不承担任何法律责任。本网站所展示的所有内容,如文字、图像、标志、音频、视频、软件和程序等的版权均属于原创作者。如果任何组织或个人认为网站内容可能侵犯其知识产权,或包含不准确之处,请即刻联系我们进行相应处理。

标签:C ++ 协议缓冲器
上一篇:将数据帧分为几个数据范围-英雄云拓展知识分享
下一篇:自定义内涵菜单内容-英雄云拓展知识分享
相关文章

 发表评论

暂时没有评论,来抢沙发吧~

×