2 #include "zipios++/zipios-config.h"
5 #include "zipios++/meta-iostreams.h"
9 #include "zipios++/zipinputstreambuf.h"
10 #include "zipios_common.h"
18 ZipInputStreambuf::ZipInputStreambuf( streambuf *inbuf, int s_pos, bool del_inbuf )
19 : InflateInputStreambuf( inbuf, s_pos, del_inbuf ),
23 void ZipInputStreambuf::closeEntry() {
27 // check if we're positioned correctly, otherwise position us correctly
28 int position = static_cast< int >( _inbuf->pubseekoff(0, ios::cur,
30 if ( position != _data_start + static_cast< int >( _curr_entry.getCompressedSize() ) )
31 _inbuf->pubseekoff(_data_start + _curr_entry.getCompressedSize(),
36 void ZipInputStreambuf::close() {
39 ConstEntryPointer ZipInputStreambuf::getNextEntry() {
43 // read the zip local header
44 istream is( _inbuf ) ; // istream does not destroy the streambuf.
45 is.exceptions( ios::eofbit | ios::failbit | ios::badbit );
49 if ( _curr_entry.isValid() ) {
50 _data_start = static_cast< int >( _inbuf->pubseekoff(0, ios::cur, ios::in) );
51 if ( _curr_entry.getMethod() == DEFLATED ) {
53 reset() ; // reset inflatestream data structures
54 // cerr << "deflated" << endl ;
55 } else if ( _curr_entry.getMethod() == STORED ) {
57 _remain = _curr_entry.getSize() ;
58 // Force underflow on first read:
59 setg( &( _outvec[ 0 ] ),
60 &( _outvec[ 0 ] ) + _outvecsize,
61 &( _outvec[ 0 ] ) + _outvecsize );
62 // cerr << "stored" << endl ;
64 _open_entry = false ; // Unsupported compression format.
65 throw FCollException( "Unsupported compression format" ) ;
72 if ( _curr_entry.isValid() && _curr_entry.trailingDataDescriptor() )
73 throw FCollException( "Trailing data descriptor in zip file not supported" ) ;
74 return new ZipLocalEntry( _curr_entry ) ;
78 ZipInputStreambuf::~ZipInputStreambuf() {
82 int ZipInputStreambuf::underflow() {
84 return EOF ; // traits_type::eof()
85 if ( _curr_entry.getMethod() == DEFLATED )
86 return InflateInputStreambuf::underflow() ;
88 // Ok, we're are stored, so we handle it ourselves.
89 int num_b = min( _remain, _outvecsize ) ;
90 int g = static_cast< int >( _inbuf->sgetn( &(_outvec[ 0 ] ) , num_b ) ) ;
91 setg( &( _outvec[ 0 ] ),
93 &( _outvec[ 0 ] ) + g ) ;
96 return static_cast< unsigned char >( *gptr() ) ;
98 return EOF ; // traits_type::eof()
102 // FIXME: We need to check somew
104 // // gp_bitfield bit 3 is one, if the length of the zip entry
105 // // is stored in a trailer.
106 // if ( is->good && ( _curr_entry.gp_bitfield & 4 ) != 1 )
117 Implementation of ZipInputStreambuf.
121 Zipios++ - a small C++ library that provides easy access to .zip files.
122 Copyright (C) 2000 Thomas Søndergaard
124 This library is free software; you can redistribute it and/or
125 modify it under the terms of the GNU Lesser General Public
126 License as published by the Free Software Foundation; either
127 version 2 of the License, or (at your option) any later version.
129 This library is distributed in the hope that it will be useful,
130 but WITHOUT ANY WARRANTY; without even the implied warranty of
131 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
132 Lesser General Public License for more details.
134 You should have received a copy of the GNU Lesser General Public
135 License along with this library; if not, write to the Free Software
136 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA