zipios++  2.0.2
Zipios++ – a small C++ library that provides easy access to .zip files.
Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
zipios::ZipOutputStreambuf Class Reference

Handle the output buffer of a zip archive. More...

#include <zipoutputstreambuf.hpp>

Inheritance diagram for zipios::ZipOutputStreambuf:
Inheritance graph
Collaboration diagram for zipios::ZipOutputStreambuf:
Collaboration graph

Public Member Functions

 ZipOutputStreambuf (std::streambuf *outbuf)
 Initialize a ZipOutputStreambuf object. More...
 ZipOutputStreambuf (ZipOutputStreambuf const &src)=delete
virtual ~ZipOutputStreambuf ()
 Clean up the buffer. More...
void close ()
 Close the output stream buffer. More...
void closeEntry ()
 Close this buffer entry. More...
void closeStream ()
 Closing the stream. More...
void finish ()
 Finish up an output stream buffer. More...
uint32_t getCrc32 () const
 Get the CRC32 of the file. More...
size_t getSize () const
 Retrieve the size of the file deflated. More...
bool init (FileEntry::CompressionLevel compression_level)
 Initialize the zlib library. More...
ZipOutputStreambufoperator= (ZipOutputStreambuf const &rhs)=delete
void putNextEntry (FileEntry::pointer_t entry)
 Start saving an entry in the output buffer. More...
void setComment (std::string const &comment)
 Set the archive comment. More...

Protected Member Functions

virtual int overflow (int c=EOF) override
 Implementation of the overflow() function. More...
virtual int sync () override
 Implement the sync() functionality. More...

Protected Attributes

std::vector< char > m_invec
std::streambuf * m_outbuf
uint32_t m_overflown_bytes = 0

Private Member Functions

void setEntryClosedState ()
 Mark the current entry as closed. More...
void updateEntryHeaderInfo ()
 Save the header information. More...

Private Attributes

FileEntry::CompressionLevel m_compression_level
FileEntry::vector_t m_entries
bool m_open = true
bool m_open_entry = false
std::string m_zip_comment

Detailed Description

The ZipOutputStreambuf class is a zip archive output streambuf filter.

Definition at line 41 of file zipoutputstreambuf.hpp.

Constructor & Destructor Documentation

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

Note that a new initialized ZipOutputStreambuf is not ready to accept data, putNextEntry() must be invoked at least once first.

[in]outbufThe streambuf to use for output.

Definition at line 92 of file zipoutputstreambuf.cpp.

zipios::ZipOutputStreambuf::ZipOutputStreambuf ( ZipOutputStreambuf const &  src)
zipios::ZipOutputStreambuf::~ZipOutputStreambuf ( )

This function cleans up this output buffer. In general this ensures that the data still cached gets flushed.

This function may gobble up some important exceptions. If you want to make sure that the file is properly written, you must call the finish() function (or the close() function) to fully terminate the file. If these functions do not fail, then the output file is considered valid and you can keep it. The finish() function can fail because of a comment or a file which are too large, for example.

Definition at line 113 of file zipoutputstreambuf.cpp.

References finish().

Member Function Documentation

void zipios::ZipOutputStreambuf::close ( )

This function calls finish to make sure that any cached data is saved and then close the stream buffer.

Definition at line 160 of file zipoutputstreambuf.cpp.

References finish().

void zipios::ZipOutputStreambuf::closeEntry ( )

Closes the current output buffer entry and positions the stream write pointer at the beginning of the next entry.

Definition at line 131 of file zipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::closeStream(), zipios::FileEntry::COMPRESSION_LEVEL_NONE, m_compression_level, m_open_entry, overflow(), setEntryClosedState(), and updateEntryHeaderInfo().

Referenced by finish(), and putNextEntry().

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 205 of file deflateoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::endDeflation(), zipios::DeflateOutputStreambuf::m_zs, and zipios::DeflateOutputStreambuf::m_zs_initialized.

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

void zipios::ZipOutputStreambuf::finish ( )

Closes the current entry (if one is open), then writes the Zip Central Directory Structure closing the ZipOutputStream. The output stream (std::ostream) that the zip archive is being written to is not closed.

Definition at line 173 of file zipoutputstreambuf.cpp.

References closeEntry(), m_entries, m_open, zipios::FilterOutputStreambuf::m_outbuf, m_zip_comment, and zipios::anonymous_namespace{zipoutputstreambuf.cpp}::writeZipCentralDirectory().

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

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.

The CRC32 of the last file that was passed through.

Definition at line 239 of file deflateoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::m_crc32.

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

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.

The uncompressed size of the file that got written here.

Definition at line 256 of file deflateoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::m_overflown_bytes.

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

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.

true if the initialization succeeded, false otherwise.

Definition at line 106 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 zipios::GZIPOutputStreambuf::GZIPOutputStreambuf(), and putNextEntry().

ZipOutputStreambuf& zipios::ZipOutputStreambuf::operator= ( ZipOutputStreambuf const &  rhs)
int zipios::ZipOutputStreambuf::overflow ( int  c = EOF)

When writing to a buffer, the overflow() function gets called when there is no more room in the output buffer. The buffer is expected to flush the data to disk and reset the buffer availability.

[in]cThe character that made it all happen. Maybe EOF.
EOF if the function fails, 0 otherwise.

Reimplemented from zipios::DeflateOutputStreambuf.

Definition at line 273 of file zipoutputstreambuf.cpp.

References zipios::FileEntry::COMPRESSION_LEVEL_NONE, zipios::getBufferSize(), m_compression_level, zipios::DeflateOutputStreambuf::m_invec, zipios::FilterOutputStreambuf::m_outbuf, zipios::DeflateOutputStreambuf::m_overflown_bytes, and zipios::DeflateOutputStreambuf::overflow().

Referenced by closeEntry().

void zipios::ZipOutputStreambuf::putNextEntry ( FileEntry::pointer_t  entry)

Opens the next entry in the zip archive and returns a const pointer to a FileEntry object for the entry.

If a previous entry was still open, the function calls closeEntry() first.

[in]entryThe entry to be saved and made current.
Rethink the design as we have to force a call to the correct write() function?

Definition at line 197 of file zipoutputstreambuf.cpp.

References closeEntry(), zipios::FileEntry::COMPRESSION_LEVEL_NONE, zipios::getBufferSize(), zipios::DeflateOutputStreambuf::init(), m_compression_level, m_entries, zipios::DeflateOutputStreambuf::m_invec, m_open_entry, zipios::FilterOutputStreambuf::m_outbuf, zipios::DeflateOutputStreambuf::m_overflown_bytes, zipios::STORED, and zipios::ZipLocalEntry::write().

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

This function saves a global comment for the Zip archive.

You may set it to an empty string which means that no comment will be saved.

The comment is saved when the first entry is saved so it has to be put in there early on.

[in]commentThe comment to save in the Zip archive.

Definition at line 253 of file zipoutputstreambuf.cpp.

References m_zip_comment.

void zipios::ZipOutputStreambuf::setEntryClosedState ( )

After the putNextEntry() call and saving of the file content, the closeEntry() function can be called to close the entry. The entry is really closed when this setEntryClosedState() is called.

Update put pointers to trigger overflow on write. Overflow should then return EOF while m_open_entry is false.

Definition at line 331 of file zipoutputstreambuf.cpp.

References m_open_entry.

Referenced by closeEntry().

int zipios::ZipOutputStreambuf::sync ( )

This virtual function is reimplemented to make sure that the system does not run a default sync() function.

This function calls the DeflateOutputStreambuf::sync() function which returns -1 because it will not "synchronize" the input buffer.

Reimplemented from zipios::DeflateOutputStreambuf.

Definition at line 318 of file zipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::sync().

void zipios::ZipOutputStreambuf::updateEntryHeaderInfo ( )

This function saves parameters that are now available in the header of the local entry.

These parameters include:

  • The uncompressed size of the entry
  • The compressed size of the entry
  • The CRC32 of the input file (before the compression)
Rethink the design as we have to force a call to the correct getHeaderSize() function?
Rethink the design as we have to force a call to the correct write() function?

Definition at line 353 of file zipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::getCrc32(), zipios::ZipLocalEntry::getHeaderSize(), zipios::DeflateOutputStreambuf::getSize(), m_entries, m_open_entry, zipios::FilterOutputStreambuf::m_outbuf, and zipios::ZipLocalEntry::write().

Referenced by closeEntry().

Member Data Documentation

FileEntry::CompressionLevel zipios::ZipOutputStreambuf::m_compression_level

Definition at line 65 of file zipoutputstreambuf.hpp.

Referenced by closeEntry(), overflow(), and putNextEntry().

FileEntry::vector_t zipios::ZipOutputStreambuf::m_entries

Definition at line 64 of file zipoutputstreambuf.hpp.

Referenced by finish(), putNextEntry(), and updateEntryHeaderInfo().

std::vector<char> zipios::DeflateOutputStreambuf::m_invec
bool zipios::ZipOutputStreambuf::m_open = true

Definition at line 67 of file zipoutputstreambuf.hpp.

Referenced by finish().

bool zipios::ZipOutputStreambuf::m_open_entry = false
std::streambuf* zipios::FilterOutputStreambuf::m_outbuf
uint32_t zipios::DeflateOutputStreambuf::m_overflown_bytes = 0
std::string zipios::ZipOutputStreambuf::m_zip_comment

Definition at line 63 of file zipoutputstreambuf.hpp.

Referenced by finish(), and setComment().

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