![]() |
Platforms, Frameworks & Libraries »
STL »
General
Intermediate
zipstream, bzip2stream: iostream wrappers for the zlib and bzip2 librariesBy Jonathan de HalleuxSTL compliant, stream-to-stream, zlib and bzip2 wrapper with wide char support. |
VC6, VC7, VC7.1Win2K, WinXP, Win2003, STL, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
This article presents two zipped STL iostream implementation based on the library zlib (see download link above) and bzip2 (see download link above). This means that you can easily manipulate zipped streams like any other STL ostream/istream.
To give you an idea, consider following snippet that prints "Hello World":
ostringstream output_buffer; // writing data output_buffer<<"Hello world"<<endl
Now, the same snippet but with zipped output using zlib:
// zip_ostream uses output_buffer as output buffer :) zip_ostream zipper( output_buffer ); // writing data as usual zipper<<"Hello world"<<endl
or using bzip2:
// zip_ostream uses output_buffer as output buffer :) bzip2_ostream bzipper( output_buffer ); // writing data as usual bzipper<<"Hello world"<<endl
As you can see adding zipped buffers into your existing applications is quite straightforward. To summarize, let's see some quick facts about zipstream and bzip2stream:
char, wchar_t support,
Writing wrappers around the zlib library is popular on CodeProject. If you search for 'zip' you will find at least 14 articles on the topic. Moreover, if you crawl on the web and especially on zlib home page, you can find dozens of other wrappers.
So why another wrapper? Well, none of the wrappers are fully STL compliant. Ok, this is not true since gzstream (see download link above) implements fstream-like STL streams. However, gzstream has three drawbacks:
gzip i/o methods: only file to buffer or buffer to file are supported,
wchar_t Last reason to write this wrapper: it is a good exercise to understand and implement iostreams.
The three drawbacks of gzstream pushed me to re-implement an STL wrapper for zlib (and later on, do some cut and paste to get bzip2 working).
This wrapper takes a user defined i/ostream to write or read compressed data. This approach is quite flexible since the user can give any stream (istringstream, ifstream, or custom stream) to store or load the compressed data.
Internally zip_stream acts as a triple buffer: the streambuf object in itself, zlib library and the user-defined stream. For example, during the compression process, the buffers are used:
streambuf object,
overflow is called, the first buffer data is sent to zlib which also buffers the data internally. If zlib outputs data, it is sent to the user-defined stream,
Some care must be taken when flushing: you must use the method zflush that will first flush the streambuf, then flush the zlib buffer, then flush the user-defined stream. Note that you should avoid flushing as it degrades compression.
Since I'm not an STL expert, I will very briefly discuss this part. There's room for a tutorial on this topic...
To implement custom iostream, you need to take the following steps:
my_streambuf, inherited from streambuf. You need to override the virtual methods sync, underflow and overflow. sync and overflow are used in output streams and underflow is used in input streams.
my_ostream, inherited from ostream. It will use my_streambuf
my_istream, inherited from istream. It will use my_streambuf as stream buffer. All the zlib classes are in the zlib_stream namespace and all the bzip2 classes are in the bzip2_stream namespace.
The two main classes of the zlib wrapper are basic_zip_ostream and basic_zip_istream which implement respectively compression and decompression and behave like classic basic_ostream and basic_istream.
Classical typedef are also provided for these classes:
zip_ostream, zip_istream for char streams
wzip_ostream, wzip_istream for wchar_t streams. The bzip2 classes have similar names, just replace zlib by bzip2: basic_zip_streambuf becomes basic_bzip2_streambuf.
This class inherits from basic_ostream:
template<
typename Elem,
typename Tr = char_traits<Elem;>,
typename ElemA = std::allocator<Elem>,
typename ByteT = unsigned char,
typename ByteAllocatorT = std::allocator<ByteT>
>
basic_zip_ostream : public basic_ostream<Elem, Tr>
where
Elem,Tr are the classical basic_ostream template parameters,
ElemA is the allocator for a Elem buffer used internally,
ByteT is the byte type used internally (you should not change that),
ByteAT is a custom allocator for a ByteT buffer used internally basic_zip_ostream(
ostream_reference ostream_,
bool is_gzip_ = false,
size_t level_ = Z_DEFAULT_COMPRESSION,
EStrategy strategy_ = DefaultStrategy,
size_t window_size_ = 15,
size_t memory_level_ = 8,
size_t buffer_size_ = 4096
);
ostream_ is a user defined output stream,
is_gzip_, true if you want to add the gzip header and footer,
level_, compression level 0, bad and faster to 9 max and slower,
strategy_, compression strategy, see EStrategy enum,
window_size_, memory_level_ are advanced zlib settings, check zlib manual,
buffer_size_, read buffer size Note that if you choose the gzip option, a header will be automatically added in the constructor and the gzip footer (CRC + data size) will be added in the destructor.
zlib and ostream): basic_zip_ostream& zflush()
This method must be called before using the compressed data! Since zlib does it's own buffering and ostream::flush is not virtual there is no way to avoid this problem.
long get_crc();
long get_in_size();
long get_out_size();typedef basic_zip_ostream<char> zip_ostream;
typedef basic_zip_ostream<wchar_t> zip_wostream;
This class inherits from basic_istream:
template<
typename Elem,
typename Tr = char_traits<Elem;>,
typename ElemA = std::allocator<Elem>,
typename ByteT = unsigned char,
typename ByteAT = std::allocator<ByteT>
>
basic_zip_istream : public basic_istream<Elem, Tr>
basic_zip_istream(
istream_reference istream_,
size_t window_size_ = 15,
size_t read_buffer_size_ = 1024 * 10,
size_t input_buffer_size_ = 1024 * 5
)
istream_, input stream containing the compressed data,
window_size_, should be compatible with compression window size,
read_buffer_size_, size of the streambuf buffer size,
input_buffer_size_, size of the zlib input buffer size gzip file: bool is_gzip() const
gzip file) bool check_crc() const
long get_crc() const;
long get_out_size() const;
long get_in_size() const;typedef basic_zip_istream<char> zip_istream; typedef basic_zip_istream<wchar_t> zip_wistream;
All the following examples are valid for both zlib and bzip2 wrappers.
ostringstream buffer;
zip_ostream zipper(buffer);
// writing stuff
zipper<<...
//flushing VERY IMPORTANT!
zipper.zflush();
// buffer.str() is ready
ofstream file("test.zip",ios::out | ios::binary);
{
zip_ostream zipper(file, true /* gzip file*/);
// writing stuff
zipper<<...
} // the stream is flushed, the destructor is called and gzip header appended
// the file is ready
istringstream buffer;
zip_istream unzipper(buffer);
// reading stuff
unzipper>>...
ifstream file("test.zip", ios::in | ios::binary);
zip_istream unzipper(file);
// reading stuff
unzipper>>...
// if the file was gzip, we can check the crc
if (unzipper.is_gzip())
std::cout<<"crc check: "<<( unzipper.check_crc() ? "ok" : "failed");
zlib is available,
#include "zlibstream.hpp" to include the headers, zlib is available,
#include "bzip2stream.hpp" to include the headers, ostream constructors
wchar_t working
zip_to_stream
These zlib and bzip2 wrappers are licensed under the zlib/libpng license.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Oct 2003 Editor: Smitha Vijayan |
Copyright 2003 by Jonathan de Halleux Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |