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-2022 Made to Order Software Corp. All Rights Reserved 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 zipios::GZIPOutputStream. 24 : * 25 : * The zipios::GZipOutputStream defines functions which handle the 26 : * saving of Zip archive content file in compressed form using 27 : * the zlib library. 28 : */ 29 : 30 : #include "gzipoutputstream.hpp" 31 : 32 : #include <fstream> 33 : 34 : 35 : namespace zipios 36 : { 37 : 38 : /** \class GZIPOutputStream 39 : * \brief A stream implementation that outputs data to a ZIP file. 40 : * 41 : * GZIPOutputStream is an ostream that writes the output to a zip file. 42 : * The interface approximates the interface of the Java GZIPOutputStream. 43 : * 44 : * It can be used with either an existing std::ostream object, or 45 : * a filename. 46 : */ 47 : 48 : 49 : 50 : /** \brief Create a ZIP output stream object. 51 : * 52 : * This constructor creates a zip stream from an existing standard 53 : * output stream. 54 : * 55 : * \warning 56 : * You must keep the output stream valid for as long as this object 57 : * exists (although this object close() function can be used to close 58 : * the \p os stream.) 59 : * 60 : * \param[in,out] os ostream to which the compressed zip archive is written. 61 : * \param[in] compression_level The compression level to use to compress. 62 : */ 63 0 : GZIPOutputStream::GZIPOutputStream(std::ostream & os, FileEntry::CompressionLevel compression_level) 64 0 : : m_ozf(std::make_unique<GZIPOutputStreambuf>(os.rdbuf(), compression_level)) 65 : { 66 0 : init(m_ozf.get()); 67 0 : } 68 : 69 : 70 : /** \brief Create a named ZIP stream for output. 71 : * 72 : * \note 73 : * The filename is not automatically saved as part of the stream. 74 : * To do so, call the setFilename() function. 75 : * 76 : * \param[in] filename Name of the file where the zip archive is to 77 : * be written. 78 : * \param[in] compression_level The compression level to use to compress. 79 : */ 80 0 : GZIPOutputStream::GZIPOutputStream(std::string const & filename, FileEntry::CompressionLevel compression_level) 81 : : std::ostream(0) 82 0 : , m_ofs(std::make_unique<std::ofstream>(filename.c_str(), std::ios::out | std::ios::binary)) 83 0 : , m_ozf(std::make_unique<GZIPOutputStreambuf>(m_ofs->rdbuf(), compression_level)) 84 : { 85 0 : init(m_ozf.get()); 86 0 : } 87 : 88 : 89 : /** \brief Destroy the output stream. 90 : * 91 : * The destructor ensures that all allocated resources get destroyed. 92 : */ 93 0 : GZIPOutputStream::~GZIPOutputStream() 94 : { 95 0 : } 96 : 97 : 98 : /** \brief Set the filename of a stream. 99 : * 100 : * This function can be used to set the name of the file being 101 : * added to this stream. 102 : * 103 : * The filename is optional. 104 : * 105 : * \param[in] filename The filename to attach to this stream. 106 : */ 107 0 : void GZIPOutputStream::setFilename(std::string const & filename) 108 : { 109 0 : m_ozf->setFilename(filename); 110 0 : } 111 : 112 : 113 : /** \brief Set a comment in the stream. 114 : * 115 : * This function can be used to add a comment to the zip file. 116 : * 117 : * A comment is optional. 118 : * 119 : * \param[in] comment The comment to attach to this stream. 120 : */ 121 0 : void GZIPOutputStream::setComment(std::string const & comment) 122 : { 123 0 : m_ozf->setComment(comment); 124 0 : } 125 : 126 : 127 : /** \brief Close the streams. 128 : * 129 : * This function closes the streams making sure that all data gets 130 : * saved in the output file. 131 : * 132 : * It is not required since destroying the object will also force 133 : * a close. 134 : */ 135 0 : void GZIPOutputStream::close() 136 : { 137 0 : m_ozf->close(); 138 0 : if(m_ofs) 139 : { 140 0 : m_ofs->close(); 141 : } 142 0 : } 143 : 144 : 145 : /** \brief Finishes the stream. 146 : * 147 : */ 148 0 : void GZIPOutputStream::finish() 149 : { 150 0 : m_ozf->finish(); 151 0 : } 152 : 153 : 154 : } // zipios namespace 155 : 156 : // Local Variables: 157 : // mode: cpp 158 : // indent-tabs-mode: nil 159 : // c-basic-offset: 4 160 : // tab-width: 4 161 : // End: 162 : 163 : // vim: ts=4 sw=4 et