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() ) ) {
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 ) {
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++ ) {
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.
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
Header file that defines ZipFile.
Header file that defines FileCollection.
A BackBuffer instance is useful for reading the last part of a file in an efficient manner...
Header file that defines some simple data types.
SimpleSmartPointer is a simple reference counting smart pointer template.
The header file for BackBuffer.
int readChunk(int &read_pointer)
Reads another chunk and returns the size of the chunk that has been read.
Specialization of ZipLocalEntry, that add fields for storing the extra information, that is only present in the entries in the zip central directory and not in the local entry headers.
An IOException is used to signal an I/O error.
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
An FCollException is used to signal a problem with a FileCollection.
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.
A concrete implementation of the abstract FileEntry base class for ZipFile entries, specifically for representing the information present in the local headers of file entries in a zip file.
static ZipFile openEmbeddedZipFile(const string &name)
virtual ~ZipFile()
Destructor.