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-2017 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
zipios::FileEntry::CompressionLevel
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
zipios::DeflateOutputStreambuf::getSize
size_t getSize() const
Retrieve the size of the file deflated.
Definition: deflateoutputstreambuf.cpp:256
zipios::GZIPOutputStreambuf::m_filename
std::string m_filename
Definition: gzipoutputstreambuf.hpp:61
zipios::GZIPOutputStreambuf::writeTrailer
void writeTrailer()
Definition: gzipoutputstreambuf.cpp:174
zipios::DeflateOutputStreambuf::init
bool init(FileEntry::CompressionLevel compression_level)
Initialize the zlib library.
Definition: deflateoutputstreambuf.cpp:106
zipios::DeflateOutputStreambuf::overflow
virtual int overflow(int c=EOF)
Handle an overflow.
Definition: deflateoutputstreambuf.cpp:275
zipios::GZIPOutputStreambuf::overflow
virtual int overflow(int c=EOF) override
Handle an overflow.
Definition: gzipoutputstreambuf.cpp:116
zipios::DeflateOutputStreambuf::sync
virtual int sync()
Synchronize the buffer.
Definition: deflateoutputstreambuf.cpp:340
zipiosexceptions.hpp
Various exceptions used throughout the Zipios++ library, all based on zipios::Exception.
zipios::GZIPOutputStreambuf::setComment
void setComment(std::string const &comment)
Definition: gzipoutputstreambuf.cpp:83
gzipoutputstreambuf.hpp
File defining zipios::GZIPOutputStreambuf.
zipios::GZIPOutputStreambuf::writeInt
void writeInt(uint32_t i)
Definition: gzipoutputstreambuf.cpp:182
zipios::GZIPOutputStreambuf::m_open
bool m_open
Definition: gzipoutputstreambuf.hpp:63
zipios::GZIPOutputStreambuf::writeHeader
void writeHeader()
Definition: gzipoutputstreambuf.cpp:134
zipios::InvalidStateException
Exception used when it is not possible to move forward.
Definition: zipiosexceptions.hpp:107
zipios::DeflateOutputStreambuf::getCrc32
uint32_t getCrc32() const
Get the CRC32 of the file.
Definition: deflateoutputstreambuf.cpp:239
zipios::GZIPOutputStreambuf::finish
void finish()
Finishes the compression.
Definition: gzipoutputstreambuf.cpp:103
zipios::GZIPOutputStreambuf::m_comment
std::string m_comment
Definition: gzipoutputstreambuf.hpp:62
zipios::DeflateOutputStreambuf::closeStream
void closeStream()
Closing the stream.
Definition: deflateoutputstreambuf.cpp:205
zipios::GZIPOutputStreambuf::close
void close()
Close the stream.
Definition: gzipoutputstreambuf.cpp:93
zipios::GZIPOutputStreambuf::~GZIPOutputStreambuf
virtual ~GZIPOutputStreambuf() override
Ensures that the stream gets closed properly.
Definition: gzipoutputstreambuf.cpp:71
zipios::DeflateOutputStreambuf
A class to handle stream deflate on the fly.
Definition: deflateoutputstreambuf.hpp:47
zipios::GZIPOutputStreambuf::sync
virtual int sync() override
Synchronize the buffer.
Definition: gzipoutputstreambuf.cpp:128
zipios::GZIPOutputStreambuf::GZIPOutputStreambuf
GZIPOutputStreambuf(std::streambuf *outbuf, FileEntry::CompressionLevel compression_level)
Initialize a GZIPOutputStreambuf object.
Definition: gzipoutputstreambuf.cpp:54
zipios::FilterOutputStreambuf::m_outbuf
std::streambuf * m_outbuf
Definition: filteroutputstreambuf.hpp:49
zipios
The zipios namespace includes the Zipios++ library definitions.
Definition: backbuffer.cpp:35
zipios::GZIPOutputStreambuf::setFilename
void setFilename(std::string const &filename)
Definition: gzipoutputstreambuf.cpp:77