112 throw std::logic_error(
"DeflateOutputStreambuf::init(): initialization function called when the class is already initialized. This is not supported.");
116 int const default_mem_level(8);
118 int zlevel(Z_NO_COMPRESSION);
119 switch(compression_level)
122 zlevel = Z_DEFAULT_COMPRESSION;
126 zlevel = Z_BEST_COMPRESSION;
130 zlevel = Z_BEST_SPEED;
134 throw std::logic_error(
"the compression level NONE is not supported in DeflateOutputStreambuf::init()");
142 throw std::logic_error(
"the compression level must be defined between -3 and 100, see the zipios++/fileentry.hpp for a list of valid levels.");
155 zlevel = ((compression_level - 1) * 8 + 11 / 2) / 99 + 1;
162 m_zs.next_in =
reinterpret_cast<unsigned char *
>(&
m_invec[0]);
165 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
172 int const err = deflateInit2(&
m_zs, zlevel, Z_DEFLATED, -MAX_WBITS, default_mem_level, Z_DEFAULT_STRATEGY);
179 std::ostringstream msgs;
180 msgs <<
"DeflateOutputStreambuf::init(): error while initializing zlib, " << zError(err) << std::endl;
214 int const err(deflateEnd(&
m_zs));
219 std::ostringstream msgs;
220 msgs <<
"DeflateOutputStreambuf::closeStream(): deflateEnd failed: " << zError(err) << std::endl;
279 m_zs.avail_in = pptr() - pbase();
280 m_zs.next_in =
reinterpret_cast<unsigned char *
>(&
m_invec[0]);
282 if(
m_zs.avail_in > 0)
286 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
290 while((
m_zs.avail_in > 0 ||
m_zs.avail_out == 0) && err == Z_OK)
292 if(
m_zs.avail_out == 0)
297 err = deflate(&
m_zs, Z_NO_FLUSH);
307 if(err != Z_OK && err != Z_STREAM_END)
316 msgs <<
"Deflation failed:" << zError(err);
359 if(deflated_bytes > 0)
362 if(deflated_bytes != bc)
367 throw IOException(
"DeflateOutputStreambuf::flushOutvec(): write to buffer failed.");
371 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
386 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
401 if(
m_zs.avail_out == 0)
406 err = deflate(&
m_zs, Z_FINISH);
417 if(err != Z_STREAM_END)
423 std::ostringstream msgs;
424 msgs <<
"DeflateOutputStreambuf::endDeflation(): deflate() failed: "
425 << zError(err) << std::endl;
A base class to develop output stream filters.
Various exceptions used throughout the Zipios++ library, all based on zipios::Exception.
virtual int overflow(int c=EOF)
Handle an overflow.
void closeStream()
Closing the stream.
void endDeflation()
End deflation of current file.
virtual int sync()
Synchronize the buffer.
bool init(FileEntry::CompressionLevel compression_level)
Initialize the zlib library.
static CompressionLevel const COMPRESSION_LEVEL_NONE
static CompressionLevel const COMPRESSION_LEVEL_DEFAULT
int CompressionLevel
The compression level to be used to save an entry.
static CompressionLevel const COMPRESSION_LEVEL_MINIMUM
An IOException is used to signal an I/O error.
Header file that defines zipios::DeflateOutputStreambuf.
void flushOutvec()
Flush the cached output data.
std::streambuf * m_outbuf
DeflateOutputStreambuf(std::streambuf *outbuf)
Initialize a DeflateOutputStreambuf object.
virtual ~DeflateOutputStreambuf()
Clean up any resources used by this object.
Various functions used throughout the library.
std::ostringstream OutputStringStream
An output stream using strings.
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
std::vector< char > m_outvec
static CompressionLevel const COMPRESSION_LEVEL_FASTEST
static CompressionLevel const COMPRESSION_LEVEL_SMALLEST
size_t getSize() const
Retrieve the size of the file deflated.
uint32_t m_overflown_bytes
std::vector< char > m_invec
uint32_t getCrc32() const
Get the CRC32 of the file.