2 #include "zipios++/zipios-config.h"
4 #include "zipios++/meta-iostreams.h"
22 ifstream ifs( name.c_str(), ios::in | ios::binary ) ;
23 ifs.seekg( -4, ios::end ) ;
24 uint32 start_offset = readUint32( ifs ) ;
26 return ZipFile( name, start_offset, 4 ) ;
32 : _vs( s_off, e_off ) {
36 ifstream _zipfile( name.c_str(), ios::in | ios::binary ) ;
62 MatchPath matchpath ) {
72 static_cast< const ZipCDirEntry * >( ent.get() )->
73 getLocalHeaderOffset() + _vs.startOffset() ) ) ;
84 bool ZipFile::init( istream &_zipfile ) {
88 setError (
"Error reading from file" ) ;
92 _valid = readCentralDirectory( _zipfile ) ;
98 bool ZipFile::readCentralDirectory ( istream &_zipfile ) {
100 if ( ! readEndOfCentralDirectory( _zipfile ) )
101 throw FCollException(
"Unable to find zip structure: End-of-central-directory" ) ;
104 _vs.vseekg( _zipfile, _eocd.offset(), ios::beg ) ;
108 _entries.resize ( _eocd.totalCount(), 0 ) ;
109 while ( ( entry_num < _eocd.totalCount() ) ) {
110 ZipCDirEntry *ent =
new ZipCDirEntry ;
111 _entries[ entry_num ] = ent ;
114 if ( _zipfile.bad() )
115 throw IOException(
"Error reading zip file while reading zip file central directory" ) ;
116 else if ( _zipfile.fail() )
117 throw FCollException(
"Zip file consistency problem. Failure while reading zip file central directory" ) ;
118 else if ( _zipfile.eof() )
119 throw IOException(
"Premature end of file while reading zip file central directory" ) ;
126 int pos = _vs.vtellg( _zipfile ) ;
127 _vs.vseekg( _zipfile, 0, ios::end ) ;
128 int remaining =
static_cast< int >( _vs.vtellg( _zipfile ) ) - pos ;
129 if ( remaining != _eocd.eocdOffSetFromEnd() )
130 throw FCollException(
"Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
134 if ( ! confirmLocalHeaders( _zipfile ) )
135 throw FCollException(
"Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
141 bool ZipFile::readEndOfCentralDirectory ( istream &_zipfile ) {
142 BackBuffer bb( _zipfile, _vs ) ;
147 if ( ! bb.readChunk ( read_p ) ) {
151 if ( _eocd.read( bb, read_p ) ) {
161 bool ZipFile::confirmLocalHeaders( istream &_zipfile ) {
162 Entries::const_iterator it ;
164 int inconsistencies = 0 ;
166 for ( it = _entries.begin() ; it != _entries.end() ; it++ ) {
167 ent =
static_cast< ZipCDirEntry *
>( (*it).get() ) ;
168 _vs.vseekg( _zipfile, ent->getLocalHeaderOffset(), ios::beg ) ;
170 if ( ! _zipfile || zlh != *ent ) {
175 return ! inconsistencies ;
178 void ZipFile::setError (
string error_str ) {
180 #ifdef _USE_EXCEPTIONS
183 cerr << error_str << endl ;
ZipFile()
Default constructor.
Header file that defines ZipFile.
Header file that defines FileCollection.
Header file that defines some simple data types.
SimpleSmartPointer is a simple reference counting smart pointer template.
The header file for BackBuffer.
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
An object member function may throw this exception, if the operation it normally performs is inapprop...
virtual istream * getInputStream(const ConstEntryPointer &entry)
virtual void close()
Closes the FileCollection.
static ZipFile openEmbeddedZipFile(const string &name)
virtual ~ZipFile()
Destructor.
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const