文件压缩来处理c ++中的中间输出 [英] file compression to handle intermediary output in c++

查看:97
本文介绍了文件压缩来处理c ++中的中间输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

我想压缩程序的中间输出(在C ++中) Boost IOStreams压缩您的数据,例如沿着这些行压缩/解压缩到/从文件(例如改编自批准文档):

  #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>

命名空间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<< 这是一个gz file\\\
;
}

{
std :: ifstream ifile(hello.gz,std :: ios_base :: in | std :: ios_base :: binary);
bo :: filtering_streambuf< bo :: input>在;
in.push(bo :: gzip_decompressor());
in.push(ifile);
boost :: iostreams :: copy(in,std :: cout);
}
}

您还可以看看Boost序列化可以使保存您的数据更容易。可以组合这两种方法(示例)。 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.push(bo :: gzip_compressor());
out.push(ofile);
bo :: copy(ifile,out);


I want to compress a intermediate output of my program ( in C++) and then decompress it.

解决方案

You can use Boost IOStreams to compress your data, for example something along these lines to compress/decompresses into/from a file (example adapted from 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);
    }
}

You can also have a look at Boost Serialization - which can make saving your data much easier. It is possible to combine the two approaches (example). IOStreams support bzip compression as well.

EDIT: To address your last comment - you can compress an existing file... but it would be better to write it as compressed to begin with. If you really want, you could tweak the following code:

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);

这篇关于文件压缩来处理c ++中的中间输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆