2
#include "zipios-config.h"
7
#include "meta-iostreams.h"
11
#include "zipoutputstreambuf.h"
21
ZipOutputStreambuf::ZipOutputStreambuf( streambuf *outbuf, bool del_outbuf )
22
: DeflateOutputStreambuf( outbuf, false, del_outbuf ),
31
void ZipOutputStreambuf::closeEntry() {
37
updateEntryHeaderInfo() ;
38
setEntryClosedState( ) ;
42
void ZipOutputStreambuf::close() {
47
void ZipOutputStreambuf::finish() {
51
ostream os( _outbuf ) ;
52
writeCentralDirectory( _entries, EndOfCentralDirectory( _zip_comment), os ) ;
57
ZipOutputStreambuf::~ZipOutputStreambuf() {
62
void ZipOutputStreambuf::putNextEntry( const ZipCDirEntry &entry ) {
66
if ( ! init( _level ) )
67
cerr << "ZipOutputStreambuf::putNextEntry(): init() failed!\n" ;
69
_entries.push_back( entry ) ;
70
ZipCDirEntry &ent = _entries.back() ;
72
ostream os( _outbuf ) ;
74
// Update entry header info
75
ent.setLocalHeaderOffset( os.tellp() ) ;
76
ent.setMethod( _method ) ;
78
os << static_cast< ZipLocalEntry >( ent ) ;
84
void ZipOutputStreambuf::setComment( const string &comment ) {
85
_zip_comment = comment ;
89
void ZipOutputStreambuf::setLevel( int level ) {
94
void ZipOutputStreambuf::setMethod( StorageMethod method ) {
96
if( method == STORED )
97
setLevel( NO_COMPRESSION ) ;
98
else if ( method == DEFLATED ) {
99
if( _level == NO_COMPRESSION )
100
setLevel( DEFAULT_COMPRESSION ) ;
102
throw FCollException( "Specified compression method not supported" ) ;
106
// Protected and private methods
109
int ZipOutputStreambuf::overflow( int c ) {
110
return DeflateOutputStreambuf::overflow( c ) ;
111
// // FIXME: implement
113
// cout << "ZipOutputStreambuf::overflow() not implemented yet!\n" ;
119
int ZipOutputStreambuf::sync() {
120
return DeflateOutputStreambuf::sync() ;
121
// // FIXME: implement
122
// cout << "ZipOutputStreambuf::sync() not implemented yet!\n" ;
128
void ZipOutputStreambuf::setEntryClosedState() {
129
_open_entry = false ;
130
// FIXME: update put pointers to trigger overflow on write. overflow
131
// should then return EOF while _open_entry is false.
135
void ZipOutputStreambuf::updateEntryHeaderInfo() {
139
ostream os( _outbuf ) ;
140
int curr_pos = os.tellp() ;
142
// update fields in _entries.back()
143
ZipCDirEntry &entry = _entries.back() ;
144
entry.setSize( getCount() ) ;
145
entry.setCrc( getCrc32() ) ;
146
entry.setCompressedSize( curr_pos - entry.getLocalHeaderOffset()
147
- entry.getLocalHeaderSize() ) ;
149
// Mark Donszelmann: added current date and time
153
now = localtime( <ime );
154
int dosTime = (now->tm_year - 80) << 25 | (now->tm_mon + 1) << 21 | now->tm_mday << 16 |
155
now->tm_hour << 11 | now->tm_min << 5 | now->tm_sec >> 1;
156
entry.setTime(dosTime);
158
// write ZipLocalEntry header to header position
159
os.seekp( entry.getLocalHeaderOffset() ) ;
160
os << static_cast< ZipLocalEntry >( entry ) ;
161
os.seekp( curr_pos ) ;
165
void ZipOutputStreambuf::writeCentralDirectory( const vector< ZipCDirEntry > &entries,
166
EndOfCentralDirectory eocd,
168
int cdir_start = os.tellp() ;
169
std::vector< ZipCDirEntry >::const_iterator it ;
172
for ( it = entries.begin() ; it != entries.end() ; ++it ) {
174
cdir_size += it->getCDirHeaderSize() ;
176
eocd.setOffset( cdir_start ) ;
177
eocd.setCDirSize( cdir_size ) ;
178
eocd.setTotalCount( entries.size() ) ;
185
Implementation of ZipOutputStreambuf.
189
Zipios++ - a small C++ library that provides easy access to .zip files.
190
Copyright (C) 2000 Thomas Søndergaard
192
This library is free software; you can redistribute it and/or
193
modify it under the terms of the GNU Lesser General Public
194
License as published by the Free Software Foundation; either
195
version 2 of the License, or (at your option) any later version.
197
This library is distributed in the hope that it will be useful,
198
but WITHOUT ANY WARRANTY; without even the implied warranty of
199
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
200
Lesser General Public License for more details.
202
You should have received a copy of the GNU Lesser General Public
203
License along with this library; if not, write to the Free Software
204
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA