プログラムの中間出力(C ++)を圧縮してから解凍したいと思います。
3135 次
1 に答える
11
Boost IOStreamsを使用してデータを圧縮できます。たとえば、これらの行に沿ってファイルに圧縮/解凍します(例はBoost docsから採用されています)。
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
namespace bo = boost::iostreams;
int main()
{
{
std::ofstream ofile("hello.gz", std::ios_base::out | std::ios_base::binary);
bo::filtering_ostream out;
out.push(bo::gzip_compressor());
out.push(ofile);
out << "This is a gz file\n";
}
{
std::ifstream ifile("hello.gz", std::ios_base::in | std::ios_base::binary);
bo::filtering_streambuf<bo::input> in;
in.push(bo::gzip_decompressor());
in.push(ifile);
boost::iostreams::copy(in, std::cout);
}
}
Boost Serializationもご覧ください。これにより、データの保存がはるかに簡単になります。2つのアプローチを組み合わせることができます(例)。IOStreamsはbzip
圧縮もサポートしています。
編集:あなたの最後のコメントに対処するために-あなたは既存のファイルを圧縮することができます...しかし、そもそも圧縮されたものとしてそれを書く方が良いでしょう。本当に必要な場合は、次のコードを微調整できます。
std::ifstream ifile("file", std::ios_base::in | std::ios_base::binary);
std::ofstream ofile("file.gz", std::ios_base::out | std::ios_base::binary);
bo::filtering_streambuf<bo::output> out;
out.push(bo::gzip_compressor());
out.push(ofile);
bo::copy(ifile, out);
于 2012-04-14T18:33:30.500 に答える