zipinputstreambuf.cpp
Go to the documentation of this file.
#include "zipios++/zipios-config.h"
#include <algorithm>
#include "zipios++/meta-iostreams.h"
#include <zlib.h>
#include "zipios++/zipinputstreambuf.h"
#include "zipios_common.h"
namespace zipios {
using std::ios ;
using std::cerr ;
using std::endl ;
ZipInputStreambuf::ZipInputStreambuf( streambuf *inbuf, int s_pos, bool del_inbuf )
: InflateInputStreambuf( inbuf, s_pos, del_inbuf ),
_open_entry( false )
{
ConstEntryPointer entry = getNextEntry() ;
if ( ! entry->isValid() ) {
; // FIXME: throw something?
}
}
void ZipInputStreambuf::closeEntry() {
if ( ! _open_entry )
return ;
// check if we're positioned correctly, otherwise position us correctly
int position = _inbuf->pubseekoff(0, ios::cur,
ios::in);
if ( position != _data_start + static_cast< int >( _curr_entry.getCompressedSize() ) )
_inbuf->pubseekoff(_data_start + _curr_entry.getCompressedSize(),
ios::beg, ios::in) ;
}
void ZipInputStreambuf::close() {
}
ConstEntryPointer ZipInputStreambuf::getNextEntry() {
if ( _open_entry )
closeEntry() ;
// read the zip local header
istream is( _inbuf ) ; // istream does not destroy the streambuf.
is.exceptions(istream::eofbit | istream::failbit | istream::badbit);
is >> _curr_entry ;
if ( _curr_entry.isValid() ) {
_data_start = _inbuf->pubseekoff(0, ios::cur,
ios::in);
if ( _curr_entry.getMethod() == DEFLATED ) {
_open_entry = true ;
reset() ; // reset inflatestream data structures
// cerr << "deflated" << endl ;
} else if ( _curr_entry.getMethod() == STORED ) {
_open_entry = true ;
_remain = _curr_entry.getSize() ;
// Force underflow on first read:
setg( &( _outvec[ 0 ] ),
&( _outvec[ 0 ] ) + _outvecsize,
&( _outvec[ 0 ] ) + _outvecsize ) ;
// cerr << "stored" << endl ;
} else {
_open_entry = false ; // Unsupported compression format.
throw FCollException( "Unsupported compression format" ) ;
}
} else {
_open_entry = false ;
}
if ( _curr_entry.isValid() && _curr_entry.trailingDataDescriptor() )
throw FCollException( "Trailing data descriptor in zip file not supported" ) ;
return new ZipLocalEntry( _curr_entry ) ;
}
ZipInputStreambuf::~ZipInputStreambuf() {
}
int ZipInputStreambuf::underflow() {
if ( ! _open_entry )
return EOF ; // traits_type::eof()
if ( _curr_entry.getMethod() == DEFLATED )
return InflateInputStreambuf::underflow() ;
// Ok, we're are stored, so we handle it ourselves.
int num_b = min( _remain, _outvecsize ) ;
int g = _inbuf->sgetn( &(_outvec[ 0 ] ) , num_b ) ;
setg( &( _outvec[ 0 ] ),
&( _outvec[ 0 ] ),
&( _outvec[ 0 ] ) + g ) ;
_remain -= g ;
if ( g > 0 )
return static_cast< unsigned char >( *gptr() ) ;
else
return EOF ; // traits_type::eof()
}
// FIXME: We need to check somew
//
// // gp_bitfield bit 3 is one, if the length of the zip entry
// // is stored in a trailer.
// if ( is->good && ( _curr_entry.gp_bitfield & 4 ) != 1 )
// return true ;
// else {
// is->clear() ;
// return false ;
// }
} // namespace
/*
Zipios++ - a small C++ library that provides easy access to .zip files.
Copyright (C) 2000 Thomas Søndergaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
zipios::FCollException
An FCollException is used to signal a problem with a FileCollection.
Definition fcollexceptions.h:31
zipios::FileEntry::isValid
virtual bool isValid() const =0
Any method or operator that initializes a FileEntry may set a flag, that specifies whether the read e...
zipios::InflateInputStreambuf::reset
bool reset(int stream_position=-1)
Resets the zlib stream and purges input and output buffers.
Definition inflateinputstreambuf.cpp:117
zipios::InflateInputStreambuf::InflateInputStreambuf
InflateInputStreambuf(streambuf *inbuf, int s_pos=-1, bool del_inbuf=false)
InflateInputStreambuf constructor.
Definition inflateinputstreambuf.cpp:18
zipios::ZipInputStreambuf::closeEntry
void closeEntry()
Closes the current entry, and positions the stream read pointer at the beginning of the next entry (i...
Definition zipinputstreambuf.cpp:29
zipios::ZipInputStreambuf::~ZipInputStreambuf
virtual ~ZipInputStreambuf()
Destructor.
Definition zipinputstreambuf.cpp:82
zipios::ZipInputStreambuf::getNextEntry
ConstEntryPointer getNextEntry()
Opens the next entry in the zip archive and returns a const pointer to a FileEntry object for the ent...
Definition zipinputstreambuf.cpp:45
zipios::ZipInputStreambuf::ZipInputStreambuf
ZipInputStreambuf(streambuf *inbuf, int s_pos=-1, bool del_inbuf=false)
ZipInputStreambuf constructor.
Definition zipinputstreambuf.cpp:18
zipios::ZipInputStreambuf::close
void close()
Closes the streambuf.
Definition zipinputstreambuf.cpp:42
zipios::ZipLocalEntry
A concrete implementation of the abstract FileEntry base class for ZipFile entries,...
Definition ziphead.h:22
zipios::ZipLocalEntry::getMethod
virtual StorageMethod getMethod() const
Returns the method used to store the entry in the FileCollection.
Definition ziphead.cpp:78
zipios::ConstEntryPointer
SimpleSmartPointer< const FileEntry > ConstEntryPointer
ConstEntryPointer is a SimpleSmartPointer for const FileEntry's.
Definition fileentry.h:37
zipinputstreambuf.h
Header file that defines ZipInputStreambuf.
zipios_common.h
Header file containing miscellaneous small functions.