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

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

#include <zipoutputstreambuf.hpp>

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

Public Member Functions

 ZipOutputStreambuf (std::streambuf *outbuf)
 Initialize a ZipOutputStreambuf object.
 
 ZipOutputStreambuf (ZipOutputStreambuf const &rhs)=delete
 
virtual ~ZipOutputStreambuf ()
 Clean up the buffer.
 
void close ()
 Close the output stream buffer.
 
void closeEntry ()
 Close this buffer entry.
 
void closeStream ()
 Closing the stream.
 
void finish ()
 Finish up an output stream buffer.
 
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.
 
ZipOutputStreambufoperator= (ZipOutputStreambuf const &rhs)=delete
 
void putNextEntry (FileEntry::pointer_t entry)
 Start saving an entry in the output buffer.
 
void setComment (std::string const &comment)
 Set the archive comment.
 

Protected Member Functions

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

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 setEntryClosedState ()
 Mark the current entry as closed.
 
void updateEntryHeaderInfo ()
 Save the header information.
 

Private Attributes

FileEntry::CompressionLevel m_compression_level = FileEntry::COMPRESSION_LEVEL_DEFAULT
 
FileEntry::vector_t m_entries = FileEntry::vector_t()
 
bool m_open = true
 
bool m_open_entry = false
 
std::vector< char > m_outvec = std::vector<char>()
 
std::string m_zip_comment = std::string()
 
z_stream m_zs = z_stream()
 
bool m_zs_initialized = false
 

Detailed Description

The ZipOutputStreambuf class is a zip archive output streambuf filter.

Definition at line 41 of file zipoutputstreambuf.hpp.

Constructor & Destructor Documentation

◆ ZipOutputStreambuf() [1/2]

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.

Parameters
[in]outbufThe streambuf to use for output.

Definition at line 95 of file zipoutputstreambuf.cpp.

◆ ZipOutputStreambuf() [2/2]

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

◆ ~ZipOutputStreambuf()

zipios::ZipOutputStreambuf::~ZipOutputStreambuf ( )
virtual

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

Warning
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 114 of file zipoutputstreambuf.cpp.

References finish().

Member Function Documentation

◆ close()

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 161 of file zipoutputstreambuf.cpp.

References finish().

◆ closeEntry()

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 132 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().

◆ 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(), closeEntry(), and zipios::GZIPOutputStreambuf::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::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 174 of file zipoutputstreambuf.cpp.

References closeEntry(), m_entries, m_open, zipios::FilterOutputStreambuf::m_outbuf, and m_zip_comment.

Referenced by ~ZipOutputStreambuf(), 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 updateEntryHeaderInfo(), and zipios::GZIPOutputStreambuf::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 updateEntryHeaderInfo(), and zipios::GZIPOutputStreambuf::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 zipios::GZIPOutputStreambuf::GZIPOutputStreambuf(), and putNextEntry().

◆ operator=()

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

◆ overflow()

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

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.

Exceptions
IOExceptionThis function generates an exception if saving the data to the output fails.
Parameters
[in]cThe character that made it all happen. Maybe EOF.
Returns
EOF if the function fails, 0 otherwise.

Reimplemented from zipios::DeflateOutputStreambuf.

Definition at line 278 of file zipoutputstreambuf.cpp.

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

Referenced by closeEntry().

◆ putNextEntry()

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.

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

Definition at line 198 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().

◆ setComment()

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.

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

Definition at line 254 of file zipoutputstreambuf.cpp.

References m_zip_comment.

◆ setEntryClosedState()

void zipios::ZipOutputStreambuf::setEntryClosedState ( )
private

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.

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

Definition at line 337 of file zipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::m_crc32, and m_open_entry.

Referenced by closeEntry().

◆ sync()

int zipios::ZipOutputStreambuf::sync ( )
overrideprotectedvirtual

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 324 of file zipoutputstreambuf.cpp.

References zipios::DeflateOutputStreambuf::sync().

◆ updateEntryHeaderInfo()

void zipios::ZipOutputStreambuf::updateEntryHeaderInfo ( )
private

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)
Todo:
Rethink the design as we have to force a call to the correct getHeaderSize() function?
Todo:
Rethink the design as we have to force a call to the correct write() function?

Definition at line 360 of file zipoutputstreambuf.cpp.

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

Referenced by closeEntry().

Member Data Documentation

◆ m_compression_level

FileEntry::CompressionLevel zipios::ZipOutputStreambuf::m_compression_level = FileEntry::COMPRESSION_LEVEL_DEFAULT
private

Definition at line 66 of file zipoutputstreambuf.hpp.

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

◆ m_crc32

uint32_t zipios::DeflateOutputStreambuf::m_crc32 = 0
protectedinherited

◆ m_entries

FileEntry::vector_t zipios::ZipOutputStreambuf::m_entries = FileEntry::vector_t()
private

Definition at line 65 of file zipoutputstreambuf.hpp.

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

◆ m_invec

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

◆ m_open

bool zipios::ZipOutputStreambuf::m_open = true
private

Definition at line 68 of file zipoutputstreambuf.hpp.

Referenced by finish().

◆ m_open_entry

bool zipios::ZipOutputStreambuf::m_open_entry = false
private

◆ 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_zip_comment

std::string zipios::ZipOutputStreambuf::m_zip_comment = std::string()
private

Definition at line 64 of file zipoutputstreambuf.hpp.

Referenced by finish(), and setComment().

◆ 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: