c++中的压缩文件IO

compressed file IO in c++

本文关键字:文件 IO 压缩 c++      更新时间:2023-10-16

我有一个模拟,在这个模拟中,我需要在代码运行时转储大量数据,通常大约在50GB左右,这通常以周为单位。现在,我正在将数据导出为legacy ASCII VTK文件。

我想知道是否有任何方法可以在数据写入磁盘时压缩数据,这样我就可以节省一些空间(我需要同时运行多个版本的代码)。如果可能的话,我更喜欢标准图书馆的东西。

如果您可以使用boost,请查看zlib过滤器压缩器和解压缩器

#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>
int main() 
{
    using namespace std;
    ifstream file("hello.z", ios_base::in | ios_base::binary);
    filtering_streambuf<input> in;
    in.push(zlib_decompressor());
    in.push(file);
    boost::iostreams::copy(in, cout);
}