107 throw std::logic_error(
"DeflateOutputStreambuf::init(): initialization function called when the class is already initialized. This is not supported.");
111 int const default_mem_level(8);
113 int zlevel(Z_NO_COMPRESSION);
114 switch(compression_level)
117 zlevel = Z_DEFAULT_COMPRESSION;
121 zlevel = Z_BEST_COMPRESSION;
125 zlevel = Z_BEST_SPEED;
129 throw std::logic_error(
"the compression level NONE is not supported in DeflateOutputStreambuf::init()");
137 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.");
150 zlevel = ((compression_level - 1) * 8 + 11 / 2) / 99 + 1;
157 m_zs.next_in =
reinterpret_cast<unsigned char *
>(&
m_invec[0]);
160 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
167 int const err = deflateInit2(&
m_zs, zlevel, Z_DEFLATED, -MAX_WBITS, default_mem_level, Z_DEFAULT_STRATEGY);
174 std::ostringstream msgs;
175 msgs <<
"DeflateOutputStreambuf::init(): error while initializing zlib, " << zError(err) << std::endl;
209 int const err(deflateEnd(&
m_zs));
214 std::ostringstream msgs;
215 msgs <<
"DeflateOutputStreambuf::closeStream(): deflateEnd failed: " << zError(err) << std::endl;
274 m_zs.avail_in = pptr() - pbase();
275 m_zs.next_in =
reinterpret_cast<unsigned char *
>(&
m_invec[0]);
277 if(
m_zs.avail_in > 0)
281 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
285 while((
m_zs.avail_in > 0 ||
m_zs.avail_out == 0) && err == Z_OK)
287 if(
m_zs.avail_out == 0)
292 err = deflate(&
m_zs, Z_NO_FLUSH);
302 if(err != Z_OK && err != Z_STREAM_END)
311 msgs <<
"Deflation failed:" << zError(err);
354 if(deflated_bytes > 0)
357 if(deflated_bytes != bc)
362 throw IOException(
"DeflateOutputStreambuf::flushOutvec(): write to buffer failed.");
366 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
381 m_zs.next_out =
reinterpret_cast<unsigned char *
>(&
m_outvec[0]);
396 if(
m_zs.avail_out == 0)
401 err = deflate(&
m_zs, Z_FINISH);
412 if(err != Z_STREAM_END)
418 std::ostringstream msgs;
419 msgs <<
"DeflateOutputStreambuf::endDeflation(): deflate() failed: "
420 << zError(err) << std::endl;
virtual ~DeflateOutputStreambuf()
Clean up any resources used by this object.
bool init(FileEntry::CompressionLevel compression_level)
Initialize the zlib library.
uint32_t getCrc32() const
Get the CRC32 of the file.
void endDeflation()
End deflation of current file.
DeflateOutputStreambuf(std::streambuf *outbuf)
Initialize a DeflateOutputStreambuf object.
uint32_t m_overflown_bytes
std::vector< char > m_outvec
void flushOutvec()
Flush the cached output data.
virtual int sync()
Synchronize the buffer.
std::vector< char > m_invec
virtual int overflow(int c=EOF)
Handle an overflow.
void closeStream()
Closing the stream.
size_t getSize() const
Retrieve the size of the file deflated.
int CompressionLevel
The compression level to be used to save an entry.
static CompressionLevel const COMPRESSION_LEVEL_MINIMUM
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
static CompressionLevel const COMPRESSION_LEVEL_DEFAULT
static CompressionLevel const COMPRESSION_LEVEL_NONE
static CompressionLevel const COMPRESSION_LEVEL_FASTEST
static CompressionLevel const COMPRESSION_LEVEL_SMALLEST
A base class to develop output stream filters.
std::streambuf * m_outbuf
An IOException is used to signal an I/O error.
Header file that defines zipios::DeflateOutputStreambuf.
The zipios namespace includes the Zipios library definitions.
std::ostringstream OutputStringStream
An output stream using strings.
Various functions used throughout the library.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.