zipios  2.1.1
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 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 
148  std::ostream os(m_outbuf) ;
149  os << static_cast<unsigned char>(0x1f); // Magic #
150  os << static_cast<unsigned char>(0x8b); // Magic #
151  os << static_cast<unsigned char>(0x08); // Deflater.DEFLATED
152  os << flg; // FLG
153  os << static_cast<unsigned char>(0x00); // MTIME
154  os << static_cast<unsigned char>(0x00); // MTIME
155  os << static_cast<unsigned char>(0x00); // MTIME
156  os << static_cast<unsigned char>(0x00); // MTIME
157  os << static_cast<unsigned char>(0x00); // XFLG
158  os << static_cast<unsigned char>(0x00); // OS
159 
160  if(!m_filename.empty())
161  {
162  os << m_filename.c_str(); // Filename
163  os << static_cast<unsigned char>(0x00);
164  }
165 
166  if(!m_comment.empty())
167  {
168  os << m_comment.c_str(); // Comment
169  os << static_cast<unsigned char>(0x00);
170  }
171 }
172 
173 
175 {
176  // write the CRC32 and Size at the end of the file
177  writeInt(getCrc32());
178  writeInt(getSize());
179 }
180 
181 
183 {
185  std::ostream os(m_outbuf);
186  os << static_cast<unsigned char>( i & 0xFF);
187  os << static_cast<unsigned char>((i >> 8) & 0xFF);
188  os << static_cast<unsigned char>((i >> 16) & 0xFF);
189  os << static_cast<unsigned char>((i >> 24) & 0xFF);
190 }
191 
192 
193 } // zipios namespace
194 
195 // Local Variables:
196 // mode: cpp
197 // indent-tabs-mode: nil
198 // c-basic-offset: 4
199 // tab-width: 4
200 // End:
201 
202 // vim: ts=4 sw=4 et
File defining zipios::GZIPOutputStreambuf.
The zipios namespace includes the Zipios++ library definitions.
Definition: backbuffer.cpp:35
Various exceptions used throughout the Zipios++ library, all based on zipios::Exception.
virtual int overflow(int c=EOF)
Handle an overflow.
void closeStream()
Closing the stream.
virtual int overflow(int c=EOF) override
Handle an overflow.
virtual int sync()
Synchronize the buffer.
void setComment(std::string const &comment)
bool init(FileEntry::CompressionLevel compression_level)
Initialize the zlib library.
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
void finish()
Finishes the compression.
void close()
Close the stream.
Exception used when it is not possible to move forward.
virtual ~GZIPOutputStreambuf() override
Ensures that the stream gets closed properly.
virtual int sync() override
Synchronize the buffer.
GZIPOutputStreambuf(std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
Initialize a GZIPOutputStreambuf object.
size_t getSize() const
Retrieve the size of the file deflated.
void setFilename(std::string const &filename)
uint32_t getCrc32() const
Get the CRC32 of the file.
A class to handle stream deflate on the fly.