简体   繁体   English

如何将多个协议缓冲区的消息写入可附加的​​压缩文件?

[英]How to write multiple protocol buffers' messages into an a appendable compressed file?

I'm using protocol buffers' CodedOutputStream and FileOutputStream to serialize multiple messages in sequence into a file like this: 我正在使用协议缓冲区的CodedOutputStream和FileOutputStream将多个消息按顺序序列化为如下文件:

// File is opened using append mode and wrapped into
// a FileOutputStream and a CodedOutputStream
bool Open(const std::string& filename,
          int buffer_size = kDefaultBufferSize) {

    file_ = open(filename.c_str(),
                 O_WRONLY | O_APPEND | O_CREAT, // open mode
                 S_IREAD | S_IWRITE | S_IRGRP | S_IROTH | S_ISUID); //file permissions

    if (file_ != -1) {
        file_ostream_ = new FileOutputStream(file_, buffer_size);
        ostream_ = new CodedOutputStream(file_ostream_);
        return true;
    } else {
        return false;
    }
}

// Code for append a new message
bool Serialize(const google::protobuf::Message& message) {
    ostream_->WriteLittleEndian32(message.ByteSize());
    return message.SerializeToCodedStream(ostream_);
}

// Code for reading a message using a FileInputStream
// wrapped into a CodedInputStream 
bool Next(google::protobuf::Message *msg) {
    google::protobuf::uint32 size;
    bool has_next = istream_->ReadLittleEndian32(&size);
    if(!has_next) {
        return false;
    } else {
        CodedInputStream::Limit msgLimit = istream_->PushLimit(size);
        if ( msg->ParseFromCodedStream(istream_) ) {
            istream_->PopLimit(msgLimit);
            return true;
        }
        return false;
    }
}

How can I do the same using a GzipOutputStream? 如何使用GzipOutputStream执行相同的操作? Can a gzip compressed file be reopened to append new messages like I do using CodedOutputStream? 是否可以重新打开gzip压缩文件以附加新消息,就像我使用CodedOutputStream一样?

I've just realized that I just need to wrap the the FileOutputStream in another GzipOutputStream like this: 我刚刚意识到我只需要将FileOutputStream包装在另一个GzipOutputStream中,如下所示:

file_ostream_ = new FileOutputStream(file_, buffer_size);
gzip_ostream_ = new GzipOutputStream(file_ostream_);
ostream_ = new CodedOutputStream(gzip_ostream_);

and when reading, just do the same: 阅读时,也要这样做:

file_istream_ = new FileInputStream(file_, buffer_size);
gzip_istream_ = new GzipInputStream(file_istream_);
istream_ = new CodedInputStream(gzip_istream_);

Close and reopen the file to append messages also works fine. 关闭并重新打开文件以附加消息也可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 协议缓冲区:如何将多个消息序列化和反序列化为文件(c ++)? - protocol buffers: how to serialize and deserialize multiple messages into a file (c++)? 协议缓冲区问题,多个序列化成二进制文件 - Protocol buffers issue, multiple serializations into a binary file 使用协议缓冲区从文件读取消息时出现问题 - Problems using Protocol Buffers to read messages from file 如何 append 多个元素到协议缓冲区中的重复字段? - How to append multiple elements to a repeatedField in Protocol buffers? 如何在while循环中将结构变量的多个实例存储到appendable.txt文件中 - How does one store multiple instances of a variable of a structure in a while loop to an appendable .txt file 如何使用协议缓冲区? - How to use protocol buffers? 在Protocol Buffers中,如何从上层目录导入文件? - In Protocol Buffers, how to import a file from the upper level directory? 如何使用Protocol Buffers将多态对象数组序列化为文件? - How to serialize an array of polymorphic objects to file using Protocol Buffers? 如何将 memory 中的多个缓冲区压缩为一个并获得其压缩大小? - How to compress multiple buffers in memory with boost into one and get its compressed size? C ++ protobuf:如何通过“SerializeToOstream()”将多个消息写入文件 - C++ protobuf: how to write multiple messages into file by “SerializeToOstream()”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM