对大数据使用提升filtering_streambuf

Using boost filtering_streambuf on large data

本文关键字:filtering streambuf 数据      更新时间:2023-10-16

我正在尝试通过filtering_streambuf使用boost gzip压缩来压缩一些数据。然后将压缩版本写入光盘。问题是数据的大小超过 10GB,我相信字符串流空间不足。假设我可以将这些数据分解成碎片,那么使用字符串流和filtering_streambuf压缩所有数据的正确方法是什么?

我尝试将数据分解为多个部分,其中我将最大块大小设置为 std::string::max_size((/2 并将多个字符串流对象推送到filtering_streambuf对象,但这似乎不是filtering_streambuf的工作方式:)我还尝试使用bio::copy((反复复制每个数据块。我附加了一个示例代码,它不是我的确切代码(无法访问它 atm(,但除了压缩是文件流之外,想法是相同的。我提到的某些东西可能实际上有效,我只是在我的代码中有一个错误,但如果是这种情况,那么我会找到这个错误。只需要知道什么被认为是压缩大量数据的正确方法。

编辑:添加了我编写的实际代码。出于某种原因,这无法编译,因为 write 不是一个有效的函数?另外,也不能声明filtering_ostream。也许这个版本的提升已经过时了?正在写入的变量是字符。

boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
out.push(boost::iostreams::gzip_compressor());
out.push(boost::iostreams::file_sink(fileName.c_str()));
out.write(&sizeof_sizet, 1);
out.write(&sizeof_int, 1);
out.write(&sizeof_double, 1);
out.write(&sizeof_Int, 1);

编辑2: 这可能就是我想要实现的目标。编译但尚未测试。

boost::iostreams::filtering_ostreambuf buf;
buf.push(boost::iostreams::gzip_compressor());
buf.push(boost::iostreams::file_sink(fileName.c_str()));
std::ostream out(&buf);
out.write(&sizeof_sizet, 1);
out.write(&sizeof_int, 1);
out.write(&sizeof_double, 1);
out.write(&sizeof_Int, 1);

使用filtering_stream而不是filtering_streambuf直接写入文件,以避免在完成之前将整个压缩结果缓冲在内存中。

#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main()
{
boost::iostreams::filtering_ostream out;
out.push(boost::iostreams::gzip_compressor());
out.push(boost::iostreams::file_sink("test.gz"));
std::string test_string("FOO BAR BAZ....n");
out.write(test_string.c_str(), test_string.size() + 1);
}

我可以运行它,然后尝试解压缩它创建的文件:

>ls test.gz
ls: test.gz: No such file or directory
>test.exe
>ls test.gz
test.gz
>gzip -cd test.gz
FOO BAR BAZ....