LCOV - code coverage report
Current view: top level - src - zipoutputstream.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 25 25 100.0 %
Date: 2019-04-24 14:10:30 Functions: 9 12 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Zipios -- a small C++ library that provides easy access to .zip files.
       3             : 
       4             :   Copyright (C) 2000-2007  Thomas Sondergaard
       5             :   Copyright (C) 2015-2019  Made to Order Software Corporation
       6             : 
       7             :   This library is free software; you can redistribute it and/or
       8             :   modify it under the terms of the GNU Lesser General Public
       9             :   License as published by the Free Software Foundation; either
      10             :   version 2.1 of the License, or (at your option) any later version.
      11             : 
      12             :   This library is distributed in the hope that it will be useful,
      13             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :   Lesser General Public License for more details.
      16             : 
      17             :   You should have received a copy of the GNU Lesser General Public
      18             :   License along with this library; if not, write to the Free Software
      19             :   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      20             : */
      21             : 
      22             : /** \file
      23             :  * \brief Implementation of the zipios::ZipOutputStream class.
      24             :  *
      25             :  * The zipios::ZipOutputStream class is used as a filter to compress
      26             :  * data being written to a Zip archive.
      27             :  */
      28             : 
      29             : #include "zipoutputstream.hpp"
      30             : #include "zipcentraldirectoryentry.hpp"
      31             : 
      32             : #include <fstream>
      33             : 
      34             : 
      35             : namespace zipios
      36             : {
      37             : 
      38             : /** \class ZipOutputStream
      39             :  * \brief A ZipOutputStream to allow for data to be compressed zlib.
      40             :  *
      41             :  * ZipOutputStream is an internal ostream implementation used to save a
      42             :  * collection of files to a Zip archive file.
      43             :  */
      44             : 
      45             : 
      46             : 
      47             : /** \brief Initialize a ZipOutputStream object.
      48             :  *
      49             :  * The ZipOutputStream constructor create an output stream that will
      50             :  * be used to save Zip data to a file.
      51             :  *
      52             :  * \param[in] os  The output stream to use to write the Zip archive.
      53             :  */
      54         243 : ZipOutputStream::ZipOutputStream(std::ostream& os)
      55             :     //: std::ostream()
      56         243 :     : m_ozf(new ZipOutputStreambuf(os.rdbuf()))
      57             : {
      58         243 :     init(m_ozf.get());
      59         243 : }
      60             : 
      61             : 
      62             : /** \brief Clean up a ZipOutputStream object.
      63             :  *
      64             :  * The destructor makes sure that all resources allocated by the
      65             :  * ZipOutputStream object.
      66             :  */
      67         243 : ZipOutputStream::~ZipOutputStream()
      68             : {
      69         243 : }
      70             : 
      71             : 
      72             : /** Closes the current entry updates its header with the relevant
      73             :   size information and positions the stream write pointer for the
      74             :   next entry header. Puts the stream in EOF state. Call
      75             :   putNextEntry() to clear the EOF stream state flag. */
      76         242 : void ZipOutputStream::closeEntry()
      77             : {
      78         242 :     m_ozf->closeEntry();
      79         242 : }
      80             : 
      81             : 
      82             : /** \brief Close the current stream.
      83             :  *
      84             :  * This function calls close() on the internal stream. After this
      85             :  * call any attempt in writing to the file will fail.
      86             :  *
      87             :  * The result is to ensure that the Zip archive file is complete
      88             :  * and all buffers flushed to file.
      89             :  */
      90         239 : void ZipOutputStream::close()
      91             : {
      92         239 :     m_ozf->close();
      93         239 : }
      94             : 
      95             : 
      96             : /** \brief Finish up the output by flushing anything left.
      97             :  *
      98             :  * This function closes the current entry (if one is open) by writing
      99             :  * the Zip Central Directory Structure closing the ZipOutputStream.
     100             :  * The output stream that the zip archive is being written to is
     101             :  * not closed.
     102             :  */
     103         242 : void ZipOutputStream::finish()
     104             : {
     105         242 :     m_ozf->finish();
     106         239 : }
     107             : 
     108             : 
     109             : /** \brief Add an entry to the output stream.
     110             :  *
     111             :  * This function saves the header of the entry and returns. The caller
     112             :  * is expected to save the actual data of the file.
     113             :  *
     114             :  * \code
     115             :  *      os.putNextEntry(entry);
     116             :  *      FileCollection::stream_pointer_t is(collection->getInputEntry(entry->getName()));
     117             :  *      os << is->rdbuf();
     118             :  * \endcode
     119             :  *
     120             :  * \warning
     121             :  * The internal class keeps a copy of the shared pointer so changing
     122             :  * the entry from the outside may affect the results and invalidate
     123             :  * the resulting Zip archive file. Since this class is now internal,
     124             :  * it should not be a major problem since it is created and destroyed
     125             :  * before the user would have a chance from doing anything to the file.
     126             :  *
     127             :  * \param[in] entry  The FileEntry to add to the output stream.
     128             :  */
     129      171467 : void ZipOutputStream::putNextEntry(FileEntry::pointer_t entry)
     130             : {
     131             :     // if we do not yet have a ZipCentralDirectoryEntry object, create
     132             :     // one from the input entry (the input entry is actually expected
     133             :     // to be a DirectoryEntry!)
     134      171467 :     ZipCentralDirectoryEntry * central_directory_entry(dynamic_cast<ZipCentralDirectoryEntry *>(entry.get()));
     135      171467 :     if(central_directory_entry == nullptr)
     136             :     {
     137      171467 :         entry.reset(new ZipCentralDirectoryEntry(*entry));
     138             :     }
     139             : 
     140      171468 :     m_ozf->putNextEntry(entry);
     141      171466 : }
     142             : 
     143             : 
     144             : /** \brief Set the global comment.
     145             :  *
     146             :  * This function is used to setup the Global Comment of the Zip archive
     147             :  * file.
     148             :  *
     149             :  * This comment is saved at the very end of the file, attached to the
     150             :  * EndOfCentralDirectory block.
     151             :  *
     152             :  * \param[in] comment  The global comment to save in the Zip archive.
     153             :  */
     154         243 : void ZipOutputStream::setComment(std::string const & comment)
     155             : {
     156         243 :     m_ozf->setComment(comment);
     157         243 : }
     158             : 
     159             : 
     160           3 : } // zipios namespace
     161             : 
     162             : // Local Variables:
     163             : // mode: cpp
     164             : // indent-tabs-mode: nil
     165             : // c-basic-offset: 4
     166             : // tab-width: 4
     167             : // End:
     168             : 
     169             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12