zipios  2.2.0
Zipios -- a small C++ library that provides easy access to .zip files.
gzipoutputstreambuf.cpp
Go to the documentation of this file.
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 
31 #include "gzipoutputstreambuf.hpp"
32 
34 
35 
36 namespace zipios
37 {
38 
55  : DeflateOutputStreambuf(outbuf)
56  //, m_open(false) -- auto-init
57 {
58  if(!init(compression_level))
59  {
60  throw InvalidStateException("GZIPOutputStreambuf::GZIPOutputStreambuf() failed initializing zlib.");
61  }
62 }
63 
64 
72 {
73  finish();
74 }
75 
76 
77 void GZIPOutputStreambuf::setFilename(std::string const& filename)
78 {
79  m_filename = filename;
80 }
81 
82 
83 void GZIPOutputStreambuf::setComment(std::string const& comment)
84 {
85  m_comment = comment;
86 }
87 
88 
94 {
95  finish();
96 }
97 
98 
104 {
105  if(!m_open)
106  {
107  return;
108  }
109  m_open = false;
110 
111  closeStream();
112  writeTrailer();
113 }
114 
115 
117 {
118  if(!m_open)
119  {
120  writeHeader();
121  m_open = true;
122  }
123 
125 }
126 
127 
129 {
131 }
132 
133 
135 {
136  unsigned char const flg(
137  (m_filename.empty() ? 0x00 : 0x08)
138  | (m_comment.empty() ? 0x00 : 0x10)
139  );
140 
150  std::ostream os(m_outbuf) ;
151  os << static_cast<unsigned char>(0x1f); // Magic #
152  os << static_cast<unsigned char>(0x8b); // Magic #
153  os << static_cast<unsigned char>(0x08); // Deflater.DEFLATED
154  os << flg; // FLG
155  os << static_cast<unsigned char>(0x00); // MTIME
156  os << static_cast<unsigned char>(0x00); // MTIME
157  os << static_cast<unsigned char>(0x00); // MTIME
158  os << static_cast<unsigned char>(0x00); // MTIME
159  os << static_cast<unsigned char>(0x00); // XFLG
160  os << static_cast<unsigned char>(0x00); // OS
161 
162  if(!m_filename.empty())
163  {
164  os << m_filename.c_str(); // Filename
165  os << static_cast<unsigned char>(0x00);
166  }
167 
168  if(!m_comment.empty())
169  {
170  os << m_comment.c_str(); // Comment
171  os << static_cast<unsigned char>(0x00);
172  }
173 }
174 
175 
177 {
178  // write the CRC32 and Size at the end of the file
179  writeInt(getCrc32());
180  writeInt(getSize());
181 }
182 
183 
185 {
187  std::ostream os(m_outbuf);
188  os << static_cast<unsigned char>( i & 0xFF);
189  os << static_cast<unsigned char>((i >> 8) & 0xFF);
190  os << static_cast<unsigned char>((i >> 16) & 0xFF);
191  os << static_cast<unsigned char>((i >> 24) & 0xFF);
192 }
193 
194 
195 } // zipios namespace
196 
197 // Local Variables:
198 // mode: cpp
199 // indent-tabs-mode: nil
200 // c-basic-offset: 4
201 // tab-width: 4
202 // End:
203 
204 // vim: ts=4 sw=4 et
A class to handle stream deflate on the fly.
bool init(FileEntry::CompressionLevel compression_level)
Initialize the zlib library.
uint32_t getCrc32() const
Get the CRC32 of the file.
virtual int sync()
Synchronize the buffer.
virtual int overflow(int c=EOF)
Handle an overflow.
void closeStream()
Closing the stream.
size_t getSize() const
Retrieve the size of the file deflated.
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
void setComment(std::string const &comment)
void finish()
Finishes the compression.
virtual int sync() override
Synchronize the buffer.
virtual ~GZIPOutputStreambuf() override
Ensures that the stream gets closed properly.
virtual int overflow(int c=EOF) override
Handle an overflow.
void close()
Close the stream.
GZIPOutputStreambuf(std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
Initialize a GZIPOutputStreambuf object.
void setFilename(std::string const &filename)
Exception used when it is not possible to move forward.
File defining zipios::GZIPOutputStreambuf.
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:36
Various exceptions used throughout the Zipios library, all based on zipios::Exception.