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::GZIPOutputStreambuf Class Reference

Save the output stream buffer. More...

#include <gzipoutputstreambuf.hpp>

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

Public Member Functions

 GZIPOutputStreambuf (std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
 Initialize a GZIPOutputStreambuf object.
 
virtual ~GZIPOutputStreambuf () override
 Ensures that the stream gets closed properly.
 
void close ()
 Close the stream.
 
void closeStream ()
 Closing the stream.
 
void finish ()
 Finishes the compression.
 
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.
 
void setComment (std::string const &comment)
 
void setFilename (std::string const &filename)
 

Protected Member Functions

virtual int overflow (int c=EOF) override
 Handle an overflow.
 
virtual int sync () override
 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.
 
void writeHeader ()
 
void writeInt (uint32_t i)
 
void writeTrailer ()
 

Private Attributes

std::string m_comment = std::string()
 
std::string m_filename = std::string()
 
bool m_open = false
 
std::vector< char > m_outvec = std::vector<char>()
 
z_stream m_zs = z_stream()
 
bool m_zs_initialized = false
 

Detailed Description

This class is used to output the data of a file in a gzip stream including the necessary header and footer.

Definition at line 41 of file gzipoutputstreambuf.hpp.

Constructor & Destructor Documentation

◆ GZIPOutputStreambuf()

zipios::GZIPOutputStreambuf::GZIPOutputStreambuf ( std::streambuf *  outbuf,
FileEntry::CompressionLevel  compression_level 
)

A newly constructed GZIPOutputStreambuf is ready to accept data.

Parameters
[in,out]outbufThe streambuf to use for output.
[in]compression_levelThe compression level to use to compress.

Definition at line 54 of file gzipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::init().

◆ ~GZIPOutputStreambuf()

zipios::GZIPOutputStreambuf::~GZIPOutputStreambuf ( )
overridevirtual

This function makes sure that the stream gets closed properly which means that the compress terminates by calling finish() and the streams get closed.

Definition at line 70 of file gzipoutputstreambuf.cpp.

References finish().

Member Function Documentation

◆ close()

void zipios::GZIPOutputStreambuf::close ( )

This function ensures that the streams get closed.

Definition at line 92 of file gzipoutputstreambuf.cpp.

References finish().

◆ closeStream()

void zipios::DeflateOutputStreambuf::closeStream ( )
inherited

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 zipios::DeflateOutputStreambuf::endDeflation(), zipios::DeflateOutputStreambuf::m_zs, and zipios::DeflateOutputStreambuf::m_zs_initialized.

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

◆ endDeflation()

void zipios::DeflateOutputStreambuf::endDeflation ( )
privateinherited

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 zipios::DeflateOutputStreambuf::flushOutvec(), zipios::getBufferSize(), zipios::DeflateOutputStreambuf::m_outvec, zipios::DeflateOutputStreambuf::m_overflown_bytes, zipios::DeflateOutputStreambuf::m_zs, and zipios::DeflateOutputStreambuf::overflow().

Referenced by zipios::DeflateOutputStreambuf::closeStream().

◆ finish()

void zipios::GZIPOutputStreambuf::finish ( )

Write whatever is still necessary and close the streams.

Definition at line 102 of file gzipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::closeStream(), m_open, and writeTrailer().

Referenced by ~GZIPOutputStreambuf(), and close().

◆ flushOutvec()

void zipios::DeflateOutputStreambuf::flushOutvec ( )
privateinherited

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, zipios::DeflateOutputStreambuf::m_outvec, and zipios::DeflateOutputStreambuf::m_zs.

Referenced by zipios::DeflateOutputStreambuf::endDeflation(), and zipios::DeflateOutputStreambuf::overflow().

◆ getCrc32()

uint32_t zipios::DeflateOutputStreambuf::getCrc32 ( ) const
inherited

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 zipios::DeflateOutputStreambuf::m_crc32.

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

◆ getSize()

size_t zipios::DeflateOutputStreambuf::getSize ( ) const
inherited

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 zipios::DeflateOutputStreambuf::m_overflown_bytes.

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

◆ init()

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

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(), zipios::DeflateOutputStreambuf::m_crc32, zipios::DeflateOutputStreambuf::m_invec, zipios::DeflateOutputStreambuf::m_outvec, zipios::DeflateOutputStreambuf::m_zs, and zipios::DeflateOutputStreambuf::m_zs_initialized.

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

◆ overflow()

int zipios::GZIPOutputStreambuf::overflow ( int  c = EOF)
overrideprotectedvirtual

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 from zipios::DeflateOutputStreambuf.

Definition at line 115 of file gzipoutputstreambuf.cpp.

References m_open, zipios::DeflateOutputStreambuf::overflow(), and writeHeader().

◆ setComment()

void zipios::GZIPOutputStreambuf::setComment ( std::string const &  comment)

Definition at line 82 of file gzipoutputstreambuf.cpp.

References m_comment.

◆ setFilename()

void zipios::GZIPOutputStreambuf::setFilename ( std::string const &  filename)

Definition at line 76 of file gzipoutputstreambuf.cpp.

References m_filename.

◆ sync()

int zipios::GZIPOutputStreambuf::sync ( )
overrideprotectedvirtual

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 from zipios::DeflateOutputStreambuf.

Definition at line 127 of file gzipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::sync().

◆ writeHeader()

void zipios::GZIPOutputStreambuf::writeHeader ( )
private
Todo:
We need to know of the last modification time instead of saving all zeros for MTIME values.
Todo:
: I am thinking that the OS should be 3 under Unices.

Definition at line 133 of file gzipoutputstreambuf.cpp.

References m_comment, m_filename, and zipios::FilterOutputStreambuf::m_outbuf.

Referenced by overflow().

◆ writeInt()

void zipios::GZIPOutputStreambuf::writeInt ( uint32_t  i)
private
Todo:
: add support for 64 bit files if it exists?

Definition at line 183 of file gzipoutputstreambuf.cpp.

References zipios::FilterOutputStreambuf::m_outbuf.

Referenced by writeTrailer().

◆ writeTrailer()

void zipios::GZIPOutputStreambuf::writeTrailer ( )
private

Member Data Documentation

◆ m_comment

std::string zipios::GZIPOutputStreambuf::m_comment = std::string()
private

Definition at line 62 of file gzipoutputstreambuf.hpp.

Referenced by setComment(), and writeHeader().

◆ m_crc32

uint32_t zipios::DeflateOutputStreambuf::m_crc32 = 0
protectedinherited

◆ m_filename

std::string zipios::GZIPOutputStreambuf::m_filename = std::string()
private

Definition at line 61 of file gzipoutputstreambuf.hpp.

Referenced by setFilename(), and writeHeader().

◆ m_invec

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

◆ m_open

bool zipios::GZIPOutputStreambuf::m_open = false
private

Definition at line 63 of file gzipoutputstreambuf.hpp.

Referenced by finish(), and overflow().

◆ m_outbuf

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

◆ m_outvec

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

◆ m_overflown_bytes

uint32_t zipios::DeflateOutputStreambuf::m_overflown_bytes = 0
protectedinherited

◆ m_zs

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

◆ m_zs_initialized

bool zipios::DeflateOutputStreambuf::m_zs_initialized = false
privateinherited

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