【问题标题】:Using boost filtering_streambuf on large data在大数据上使用 boost filtering_streambuf
【发布时间】:2019-09-12 02:17:43
【问题描述】:

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

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

编辑:添加了我编写的实际代码。出于某种原因,这不会编译,因为 write 不是一个有效的函数?此外,也不能声明 filter_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);

【问题讨论】:

  • 您是否检查过内存是否用完并进行磁盘交换?此外,一些压缩算法可能会强调缓存限制。
  • "压缩后的版本然后写入光盘" -- 那么,为什么不直接压缩成文件流呢?
  • @DanMašek 嗯...不知道你能做到这一点。让我修补一下,看看会发生什么。
  • 使用filtering_stream 代替filtering_streambuf。其余与您发布的第一个代码示例相同。喜欢here。

标签: c++ boost compression stringstream boost-iostreams


【解决方案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....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2011-07-24
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多