2016-08-16 10 views
1

私は圧縮ファイルのセットを持っています。私はすべてのファイルを解凍して1つの大きなファイルを作成する必要があります。下のコードは正常に動作していますが、ファイルが大きく、ファイルコンテンツの中間コピーを作成したくないため、std :: stringstreamを使用したくありません。ブーストを使用して複数のファイルを1つのファイルに解凍する

boost::iostreams::copy(inbuf, tempfile);を直接使用しようとすると、ファイル(tmpfile)が自動的に閉じられます。コンテンツをコピーする方法はありますか?少なくとも、このファイルを自動的に閉じないようにすることはできますか?

std::ofstream tempfile("/tmp/extmpfile", std::ios::binary); 
for (set<std::string>::iterator it = files.begin(); it != files.end(); ++it) 
{ 
    string filename(*it); 
    std::ifstream gzfile(filename.c_str(), std::ios::binary); 

    boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf; 
    inbuf.push(boost::iostreams::gzip_decompressor()); 
    inbuf.push(gzfile); 

    //closes tempfile automatically!! 
    //boost::iostreams::copy(inbuf, tempfile); 

    std::stringstream out; 
    boost::iostreams::copy(inbuf, out); 
    tempfile << out.str(); 
} 
tempfile.close(); 
+0

目的のファイルの上に簡単な出力フィルタを使用しますか? –

答えて

1

Boost IOStreamsにストリームを閉じてはならないことを知らせる方法があることは知っています。あなたはstd::ostreamの代わりにboost::iostream::stream<>を使用する必要があると思います。

echo a > a 
echo b > b 
gzip a b 
などのサンプルデータで

#include <boost/iostreams/stream.hpp> 
#include <boost/iostreams/copy.hpp> 
#include <boost/iostreams/filtering_streambuf.hpp> 
#include <boost/iostreams/filter/gzip.hpp> 
#include <set> 
#include <string> 
#include <iostream> 
#include <fstream> 

int main() { 
    std::filebuf tempfilebuf; 
    tempfilebuf.open("/tmp/extmpfile", std::ios::binary|std::ios::out); 

    std::set<std::string> files { "a.gz", "b.gz" }; 
    for (std::set<std::string>::iterator it = files.begin(); it != files.end(); ++it) 
    { 
     std::string filename(*it); 
     std::ifstream gzfile(filename.c_str(), std::ios::binary); 

     boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf; 
     inbuf.push(boost::iostreams::gzip_decompressor()); 
     inbuf.push(gzfile); 

     std::ostream tempfile(&tempfilebuf); 
     boost::iostreams::copy(inbuf, tempfile); 
    } 
    tempfilebuf.close(); 
} 

Live On Coliru

:仕事に表示されます

私の簡単な回避策は、単一std::filebufオブジェクトに関連付けられた一時std::ostreamを使用していました

Gen extmpfileが含まれています

a 
b 
関連する問題