zipios 2.3.4
Zipios -- a small C++ library providing easy access to .zip files.
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
zipios::DeflateOutputStreambuf Class Reference

A class to handle stream deflate on the fly. More...

#include <deflateoutputstreambuf.hpp>

Inheritance diagram for zipios::DeflateOutputStreambuf:
Inheritance graph
[legend]
Collaboration diagram for zipios::DeflateOutputStreambuf:
Collaboration graph
[legend]

Public Member Functions

 DeflateOutputStreambuf (DeflateOutputStreambuf const &rhs)=delete
 
 DeflateOutputStreambuf (std::streambuf *outbuf)
 Initialize a DeflateOutputStreambuf object.
 
virtual ~DeflateOutputStreambuf ()
 Clean up any resources used by this object.
 
void closeStream ()
 Closing the stream.
 
uint32_t getCrc32 () const
 Get the CRC32 of the file.
 
size_t getSize () const
 Retrieve the size of the file deflated.
 
bool init (FileEntry::CompressionLevel compression_level)
 Initialize the zlib library.
 
DeflateOutputStreambufoperator= (DeflateOutputStreambuf const &rhs)=delete
 

Protected Member Functions

virtual int overflow (int c=EOF)
 Handle an overflow.
 
virtual int sync ()
 Synchronize the buffer.
 

Protected Attributes

uint32_t m_crc32 = 0
 
std::vector< char > m_invec = std::vector<char>()
 
std::streambuf * m_outbuf = nullptr
 
uint32_t m_overflown_bytes = 0
 

Private Member Functions

void endDeflation ()
 End deflation of current file.
 
void flushOutvec ()
 Flush the cached output data.
 

Private Attributes

std::vector< char > m_outvec = std::vector<char>()
 
z_stream m_zs = z_stream()
 
bool m_zs_initialized = false
 

Detailed Description

DeflateOutputStreambuf is an output stream filter, that deflates the data that is written to it before it passes it on to the output stream it is attached to. Deflation/Inflation is a compression/decompression method used in gzip and zip. The zlib library is used to perform the actual deflation, this class only wraps the functionality in an output stream filter.

Definition at line 47 of file deflateoutputstreambuf.hpp.

Constructor & Destructor Documentation

◆ DeflateOutputStreambuf() [1/2]

zipios::DeflateOutputStreambuf::DeflateOutputStreambuf ( std::streambuf *  outbuf)

This function initializes the DeflateOutputStreambuf object to make it ready for compressing data using the zlib library.

Parameters
[in,out]outbufThe streambuf to use for output.

Definition at line 57 of file deflateoutputstreambuf.cpp.

◆ DeflateOutputStreambuf() [2/2]

zipios::DeflateOutputStreambuf::DeflateOutputStreambuf ( DeflateOutputStreambuf const &  rhs)
delete

◆ ~DeflateOutputStreambuf()

zipios::DeflateOutputStreambuf::~DeflateOutputStreambuf ( )
virtual

The destructor makes sure that the zlib library is done with all the input and output data by calling various flush functions. It then makes sure that the remaining data from zlib is printed in the output file.

This is similar to calling closeStream() explicitly.

Definition at line 78 of file deflateoutputstreambuf.cpp.

References closeStream().

Member Function Documentation

◆ closeStream()

void zipios::DeflateOutputStreambuf::closeStream ( )

This function is expected to be called once the stream is getting closed (the buffer is destroyed.)

It ensures that the zlib library last few bytes get flushed and then mark the class as closed.

Note that this function can be called to close the current zlib library stream and start a new one. It is actually called from the putNextEntry() function (via the closeEntry() function.)

Definition at line 200 of file deflateoutputstreambuf.cpp.

References endDeflation(), m_zs, and m_zs_initialized.

Referenced by ~DeflateOutputStreambuf(), zipios::ZipOutputStreambuf::closeEntry(), and zipios::GZIPOutputStreambuf::finish().

◆ endDeflation()

void zipios::DeflateOutputStreambuf::endDeflation ( )
private

This function flushes the remaining data in the zlib buffers, after which the only possible operations are deflateEnd() or deflateReset().

Definition at line 377 of file deflateoutputstreambuf.cpp.

References flushOutvec(), zipios::getBufferSize(), m_outvec, m_overflown_bytes, m_zs, and overflow().

Referenced by closeStream().

◆ flushOutvec()

void zipios::DeflateOutputStreambuf::flushOutvec ( )
private

This function flushes m_outvec and updates the output pointer and size m_zs.next_out and m_zs.avail_out.

Todo:
We need to redesign the class to allow for STORED files to flow through without the need to have this crap of bytes to skip...

Definition at line 346 of file deflateoutputstreambuf.cpp.

References zipios::getBufferSize(), zipios::FilterOutputStreambuf::m_outbuf, m_outvec, and m_zs.

Referenced by endDeflation(), and overflow().

◆ getCrc32()

uint32_t zipios::DeflateOutputStreambuf::getCrc32 ( ) const

This function returns the CRC32 for the current file.

The returned value is the CRC for the data that has been compressed already (due to calls to overflow()). As DeflateOutputStreambuf may buffer an arbitrary amount of bytes until closeStream() has been invoked, the returned value is not very useful before closeStream() has been called.

Returns
The CRC32 of the last file that was passed through.

Definition at line 234 of file deflateoutputstreambuf.cpp.

References m_crc32.

Referenced by zipios::ZipOutputStreambuf::updateEntryHeaderInfo(), and zipios::GZIPOutputStreambuf::writeTrailer().

◆ getSize()

size_t zipios::DeflateOutputStreambuf::getSize ( ) const

This function returns the number of bytes written to the streambuf object and that were processed from the input buffer by the compressor. After closeStream() has been called this number is the total number of bytes written to the stream. In other words, the size of the uncompressed data.

Returns
The uncompressed size of the file that got written here.

Definition at line 251 of file deflateoutputstreambuf.cpp.

References m_overflown_bytes.

Referenced by zipios::ZipOutputStreambuf::updateEntryHeaderInfo(), and zipios::GZIPOutputStreambuf::writeTrailer().

◆ init()

bool zipios::DeflateOutputStreambuf::init ( FileEntry::CompressionLevel  compression_level)

This method is called in the constructor, so it must not write anything to the output streambuf m_outbuf (see notice in constructor.)

It will initialize the output stream as required to accept data to be compressed using the zlib library. The compression level is expected to come from the FileEntry which is about to be saved in the file.

Parameters
[in]compression_levelThe level of compression. A number from 1 to 100 or a special number representing the best, minimum, maximum compression available.
Returns
true if the initialization succeeded, false otherwise.

Definition at line 101 of file deflateoutputstreambuf.cpp.

References zipios::FileEntry::COMPRESSION_LEVEL_DEFAULT, zipios::FileEntry::COMPRESSION_LEVEL_FASTEST, zipios::FileEntry::COMPRESSION_LEVEL_MAXIMUM, zipios::FileEntry::COMPRESSION_LEVEL_MINIMUM, zipios::FileEntry::COMPRESSION_LEVEL_NONE, zipios::FileEntry::COMPRESSION_LEVEL_SMALLEST, zipios::getBufferSize(), m_crc32, m_invec, m_outvec, m_zs, and m_zs_initialized.

Referenced by zipios::GZIPOutputStreambuf::GZIPOutputStreambuf(), and zipios::ZipOutputStreambuf::putNextEntry().

◆ operator=()

DeflateOutputStreambuf & zipios::DeflateOutputStreambuf::operator= ( DeflateOutputStreambuf const &  rhs)
delete

◆ overflow()

int zipios::DeflateOutputStreambuf::overflow ( int  c = EOF)
protectedvirtual

This function is called by the streambuf implementation whenever "too many bytes" are in the output buffer, ready to be compressed.

Exceptions
IOExceptionThis exception is raised whenever the overflow() function calls a zlib library function which returns an error.
Parameters
[in]cThe character (byte) that overflowed the buffer.
Returns
Always zero (0).

Reimplemented in zipios::GZIPOutputStreambuf, and zipios::ZipOutputStreambuf.

Definition at line 270 of file deflateoutputstreambuf.cpp.

References flushOutvec(), zipios::getBufferSize(), m_crc32, m_invec, m_outvec, and m_zs.

Referenced by endDeflation(), zipios::GZIPOutputStreambuf::overflow(), and zipios::ZipOutputStreambuf::overflow().

◆ sync()

int zipios::DeflateOutputStreambuf::sync ( )
protectedvirtual

The sync() function is expected to clear the input buffer so that any new data read from the input (i.e. a file) are re-read from disk. However, a call to sync() could break the filtering functionality so we do not implement it at all.

This means you are stuck with the existing buffer. But to make sure the system understands that, we always returns -1.

Reimplemented in zipios::GZIPOutputStreambuf, and zipios::ZipOutputStreambuf.

Definition at line 335 of file deflateoutputstreambuf.cpp.

Referenced by zipios::GZIPOutputStreambuf::sync(), and zipios::ZipOutputStreambuf::sync().

Member Data Documentation

◆ m_crc32

uint32_t zipios::DeflateOutputStreambuf::m_crc32 = 0
protected

◆ m_invec

std::vector<char> zipios::DeflateOutputStreambuf::m_invec = std::vector<char>()
protected

◆ m_outbuf

std::streambuf* zipios::FilterOutputStreambuf::m_outbuf = nullptr
protectedinherited

◆ m_outvec

std::vector<char> zipios::DeflateOutputStreambuf::m_outvec = std::vector<char>()
private

Definition at line 76 of file deflateoutputstreambuf.hpp.

Referenced by endDeflation(), flushOutvec(), init(), and overflow().

◆ m_overflown_bytes

uint32_t zipios::DeflateOutputStreambuf::m_overflown_bytes = 0
protected

◆ m_zs

z_stream zipios::DeflateOutputStreambuf::m_zs = z_stream()
private

Definition at line 73 of file deflateoutputstreambuf.hpp.

Referenced by closeStream(), endDeflation(), flushOutvec(), init(), and overflow().

◆ m_zs_initialized

bool zipios::DeflateOutputStreambuf::m_zs_initialized = false
private

Definition at line 74 of file deflateoutputstreambuf.hpp.

Referenced by closeStream(), and init().


The documentation for this class was generated from the following files: