zipios
2.2.0
Zipios -- a small C++ library that provides easy access to .zip files.
src
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
33
#include "
zipios/zipiosexceptions.hpp
"
34
35
36
namespace
zipios
37
{
38
54
GZIPOutputStreambuf::GZIPOutputStreambuf
(std::streambuf *outbuf,
FileEntry::CompressionLevel
compression_level)
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
71
GZIPOutputStreambuf::~GZIPOutputStreambuf
()
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
93
void
GZIPOutputStreambuf::close
()
94
{
95
finish
();
96
}
97
98
103
void
GZIPOutputStreambuf::finish
()
104
{
105
if
(!
m_open
)
106
{
107
return
;
108
}
109
m_open
=
false
;
110
111
closeStream
();
112
writeTrailer
();
113
}
114
115
116
int
GZIPOutputStreambuf::overflow
(
int
c)
117
{
118
if
(!
m_open
)
119
{
120
writeHeader
();
121
m_open
=
true
;
122
}
123
124
return
DeflateOutputStreambuf::overflow
(c);
125
}
126
127
128
int
GZIPOutputStreambuf::sync
()
129
{
130
return
DeflateOutputStreambuf::sync
();
131
}
132
133
134
void
GZIPOutputStreambuf::writeHeader
()
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
176
void
GZIPOutputStreambuf::writeTrailer
()
177
{
178
// write the CRC32 and Size at the end of the file
179
writeInt
(
getCrc32
());
180
writeInt
(
getSize
());
181
}
182
183
184
void
GZIPOutputStreambuf::writeInt
(uint32_t i)
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
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:176
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:184
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:108
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:48
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:36
zipios::GZIPOutputStreambuf::setFilename
void setFilename(std::string const &filename)
Definition:
gzipoutputstreambuf.cpp:77
Generated on Mon Aug 10 2020 00:00:00 for zipios by
1.8.20