zipios 2.3.4
Zipios -- a small C++ library providing 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-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
32
34
35
36namespace zipios
37{
38
56{
57 if(!init(compression_level))
58 {
59 throw InvalidStateException("GZIPOutputStreambuf::GZIPOutputStreambuf() failed initializing zlib.");
60 }
61}
62
63
74
75
76void GZIPOutputStreambuf::setFilename(std::string const & filename)
77{
78 m_filename = filename;
79}
80
81
82void GZIPOutputStreambuf::setComment(std::string const & comment)
83{
84 m_comment = comment;
85}
86
87
96
97
103{
104 if(!m_open)
105 {
106 return;
107 }
108 m_open = false;
109
110 closeStream();
111 writeTrailer();
112}
113
114
116{
117 if(!m_open)
118 {
119 writeHeader();
120 m_open = true;
121 }
122
124}
125
126
131
132
134{
135 unsigned char const flg(
136 (m_filename.empty() ? 0x00 : 0x08)
137 | (m_comment.empty() ? 0x00 : 0x10)
138 );
139
149 std::ostream os(m_outbuf);
150 os << static_cast<unsigned char>(0x1f); // Magic #
151 os << static_cast<unsigned char>(0x8b); // Magic #
152 os << static_cast<unsigned char>(0x08); // Deflater.DEFLATED
153 os << flg; // FLG
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); // MTIME
158 os << static_cast<unsigned char>(0x00); // XFLG
159 os << static_cast<unsigned char>(0x00); // OS
160
161 if(!m_filename.empty())
162 {
163 os << m_filename.c_str(); // Filename
164 os << static_cast<unsigned char>(0x00);
165 }
166
167 if(!m_comment.empty())
168 {
169 os << m_comment.c_str(); // Comment
170 os << static_cast<unsigned char>(0x00);
171 }
172}
173
174
176{
177 // write the CRC32 and Size at the end of the file
179 writeInt(getSize());
180}
181
182
184{
186 std::ostream os(m_outbuf);
187 os << static_cast<unsigned char>( i & 0xFF);
188 os << static_cast<unsigned char>((i >> 8) & 0xFF);
189 os << static_cast<unsigned char>((i >> 16) & 0xFF);
190 os << static_cast<unsigned char>((i >> 24) & 0xFF);
191}
192
193
194} // zipios namespace
195
196// Local Variables:
197// mode: cpp
198// indent-tabs-mode: nil
199// c-basic-offset: 4
200// tab-width: 4
201// End:
202
203// 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.
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.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.