
Marcus Alanen wrote:
Hello, I've recently used iostreams for easier gziping and bzip2ing of output streams. However, I'd also like to be able to read any file compressed with either gzip or bzip2, by analyzing the input stream and deducing which decompressor to use.
I don't want to open the file twice or to seek in the stream, as this might not be possible due to e.g. reading from the network. My solution is to create a custom streambuf, which is told to read and retain the first few bytes to determine which decompressor to push onto the boost::iostreams::filtering_streambuf. Then the actual reading can take place, and the custom streambuf first returns the retained bytes, then just streams the rest of the actual input streambuf.
This is a nice idea. IMO, the best way to implement it would be as a filter -- perhaps you could call it a stream_signature_filter. You might use it as follows: stream_signature_filter f; f.push("GZIP", gzip_decompressor()); f.push("BZh", bzip2_decompressor()); filtering_istreambuf in(f); in.push(file("archive.tar.gz")); You could then define filters derived from stream_signature_filter that have preset mappings from signatures to filters. I'll add this to my list of ideas of 1.34. Thanks!
Regards, Marcus
Jonathan