4 #include "zipios++/zipios-config.h"
7 #include "zipios++/meta-iostreams.h"
11 #include "zipios++/zipoutputstreambuf.h"
20 ZipOutputStreambuf::ZipOutputStreambuf( streambuf *outbuf, bool del_outbuf )
21 : DeflateOutputStreambuf( outbuf, false, del_outbuf ),
30 void ZipOutputStreambuf::closeEntry() {
36 updateEntryHeaderInfo() ;
37 setEntryClosedState( ) ;
41 void ZipOutputStreambuf::close() {
46 void ZipOutputStreambuf::finish() {
50 ostream os( _outbuf ) ;
51 writeCentralDirectory( _entries, EndOfCentralDirectory( _zip_comment), os ) ;
56 ZipOutputStreambuf::~ZipOutputStreambuf() {
61 void ZipOutputStreambuf::putNextEntry( const ZipCDirEntry &entry ) {
65 if ( ! init( _level ) )
66 cerr << "ZipOutputStreambuf::putNextEntry(): init() failed!\n" ;
68 _entries.push_back( entry ) ;
69 ZipCDirEntry &ent = _entries.back() ;
71 ostream os( _outbuf ) ;
73 // Update entry header info
74 ent.setLocalHeaderOffset( static_cast< uint32 >( os.tellp() ) ) ;
75 ent.setMethod( _method ) ;
77 os << static_cast< ZipLocalEntry >( ent ) ;
83 void ZipOutputStreambuf::setComment( const string &comment ) {
84 _zip_comment = comment ;
88 void ZipOutputStreambuf::setLevel( int level ) {
93 void ZipOutputStreambuf::setMethod( StorageMethod method ) {
95 if( method == STORED )
96 setLevel( NO_COMPRESSION ) ;
97 else if ( method == DEFLATED ) {
98 if( _level == NO_COMPRESSION )
99 setLevel( DEFAULT_COMPRESSION ) ;
101 throw FCollException( "Specified compression method not supported" ) ;
105 // Protected and private methods
108 int ZipOutputStreambuf::overflow( int c ) {
109 return DeflateOutputStreambuf::overflow( c ) ;
110 // // FIXME: implement
112 // cout << "ZipOutputStreambuf::overflow() not implemented yet!\n" ;
118 int ZipOutputStreambuf::sync() {
119 return DeflateOutputStreambuf::sync() ;
120 // // FIXME: implement
121 // cout << "ZipOutputStreambuf::sync() not implemented yet!\n" ;
127 void ZipOutputStreambuf::setEntryClosedState() {
128 _open_entry = false ;
129 // FIXME: update put pointers to trigger overflow on write. overflow
130 // should then return EOF while _open_entry is false.
134 void ZipOutputStreambuf::updateEntryHeaderInfo() {
138 ostream os( _outbuf ) ;
139 int curr_pos = static_cast< int >( os.tellp() ) ;
141 // update fields in _entries.back()
142 ZipCDirEntry &entry = _entries.back() ;
143 entry.setSize( getCount() ) ;
144 entry.setCrc( getCrc32() ) ;
145 entry.setCompressedSize( curr_pos - entry.getLocalHeaderOffset()
146 - entry.getLocalHeaderSize() ) ;
148 // Mark Donszelmann: added current date and time
152 now = localtime( <ime );
153 int dosTime = (now->tm_year - 80) << 25 | (now->tm_mon + 1) << 21 | now->tm_mday << 16 |
154 now->tm_hour << 11 | now->tm_min << 5 | now->tm_sec >> 1;
155 entry.setTime(dosTime);
157 // write ZipLocalEntry header to header position
158 os.seekp( entry.getLocalHeaderOffset() ) ;
159 os << static_cast< ZipLocalEntry >( entry ) ;
160 os.seekp( curr_pos ) ;
164 void ZipOutputStreambuf::writeCentralDirectory( const vector< ZipCDirEntry > &entries,
165 EndOfCentralDirectory eocd,
167 int cdir_start = static_cast< int >( os.tellp() ) ;
168 std::vector< ZipCDirEntry >::const_iterator it ;
171 for ( it = entries.begin() ; it != entries.end() ; ++it ) {
173 cdir_size += it->getCDirHeaderSize() ;
175 eocd.setOffset( cdir_start ) ;
176 eocd.setCDirSize( cdir_size ) ;
177 eocd.setTotalCount( static_cast< uint16 >( entries.size() ) ) ;
184 Implementation of ZipOutputStreambuf.
188 Zipios++ - a small C++ library that provides easy access to .zip files.
189 Copyright (C) 2000 Thomas Søndergaard
191 This library is free software; you can redistribute it and/or
192 modify it under the terms of the GNU Lesser General Public
193 License as published by the Free Software Foundation; either
194 version 2 of the License, or (at your option) any later version.
196 This library is distributed in the hope that it will be useful,
197 but WITHOUT ANY WARRANTY; without even the implied warranty of
198 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
199 Lesser General Public License for more details.
201 You should have received a copy of the GNU Lesser General Public
202 License along with this library; if not, write to the Free Software
203 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA