【发布时间】: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