带有 GZIP 压缩的原型
Protobuf with GZIP compression
我在尝试使用GZIP
压缩将unsigned char
数组序列化为文件时遇到问题,同时使用protobuf
使用库。
我认为问题可能与我的一些语法或对 API 的滥用有关。 我也试过std::fstream
. 仅供参考,Windows 8.1和VS2013是建筑环境。
场景原型
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 <googleprotobufiozero_copy_stream_impl.h>
#include <googleprotobufiogzip_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;
}
您的代码中似乎有拼写错误。压缩级别根据 0-9 范围内的文档。将压缩级别错误地设置为 10。
当更正为:
unsigned int const _COMPRESSION_LEVEL = 9;
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- C++中高效的大型稀疏块压缩线性方程
- 嵌入方指针压缩已禁用
- C++使用整数的压缩数组初始化对象
- 在C++中将函数压缩为两种方式
- 在C++中使用LZ4压缩目录
- 使用C++进行运行长度解压缩
- 功能原型的目的
- getline() 的原型/库是什么;
- 为什么大多数 pair 实现默认不使用压缩(空基优化)?
- 具有enable_if外部类原型的模板类构造函数定义
- 函数如何通知用户它基于函数原型抛出异常?
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- C++ 如何将数组值解压缩为函数参数
- struct.error:解压缩 C++ 结构时,解包需要 288 字节的缓冲区
- 在C++中包含原型文件的正确方法是什么?
- 在 Qt(C++) 中使用 QProcess 解压缩 - 提取目录问题
- 在函数中拥有函数原型的目的是什么?
- 浏览压缩文件与游览解压缩它们
- 带有 GZIP 压缩的原型