a compete demo of iostream filter chain
#include <sys/stat.h>#include <fcntl.h>#include <sstream>#include <string>#include <list>#include <boost/iostreams/concepts.hpp>#include <boost/filesystem.hpp>#include <boost/iostreams/copy.hpp>#include <boost/filesystem/exception.hpp>#include <boost/filesystem/operations.hpp>#include <boost/iostreams/device/file_descriptor.hpp>#include <boost/iostreams/filtering_stream.hpp>#include <boost/iostreams/filter/gzip.hpp>#include <boost/iostreams/device/file.hpp>#include <boost/iostreams/stream.hpp>#include <time.h>#include <unistd.h>#include <stdio.h>#include <iostream>using namespace std;namespace fs = boost::filesystem;namespace io = boost::iostreams;struct opipestream : io::stream< io::file_descriptor_sink >{typedef io::stream< io::file_descriptor_sink > base ;explicit opipestream( const char* command ):base( fileno( pipe = popen( command, "w" ) ) ) {}~opipestream() { close() ; pclose( pipe ) ; }private : FILE* pipe ;};class mydev : public io::sink {public:std::streamsize write(const char* s, std::streamsize n) {cout<<s;(*opipe) << s;}mydev(const char* cmd){opipe = new opipestream(cmd);}~mydev(){delete opipe;}private:opipestream *opipe;};int unpack(const char* src_file){fs::path out_dir = "/tmp/myunpack";if (fs::exists(out_dir)) {fs::remove_all(out_dir);}fs::create_directory(out_dir);string cmd = "/bin/tar xf - ";ifstream file(src_file, ios_base::in | ios_base::binary);io::filtering_streambuf<io::input> in;in.push(io::gzip_decompressor());in.push(file);//mydev out(cmd.c_str());FILE* tar = popen(cmd.c_str(),"w");io::file_descriptor_sink out(fileno(tar));io::copy(in,out);fclose(tar);return -1;}int main(){return unpack("./cabal.tar.gz");} A complete demo of iostream
页:
[1]