2016-08-16 8 views
1

압축 파일 세트가 있습니다. 모든 파일을 압축 해제하고 하나의 큰 파일을 만들어야합니다. 아래 코드는 잘 작동하지만 파일이 크고 파일 내용의 중간 복사본을 만들고 싶지 않기 때문에 std :: stringstream을 사용하고 싶지 않습니다.부스트를 사용하여 하나의 파일로 여러 파일 압축 해제

boost::iostreams::copy(inbuf, tempfile);을 직접 사용하려고하면 파일 (tmpfile)이 자동으로 닫힙니다. 콘텐츠를 복사하는 더 좋은 방법이 있습니까? 또는 적어도,이 파일을 자동으로 닫지 않도록 할 수 있습니까?

+0

대상 파일 상단에 간단한 출력 필터를 사용합니까? –

답변

1

부스트 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를 사용하는 것이 었습니다

extmpfile 포함

a 
b