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 ) {
73 getLocalHeaderOffset() + _vs.startOffset() ) ) ;
83bool ZipFile::init( istream &_zipfile ) {
87 setError (
"Error reading from file" ) ;
91 _valid = readCentralDirectory( _zipfile ) ;
97bool ZipFile::readCentralDirectory ( istream &_zipfile ) {
99 if ( ! readEndOfCentralDirectory( _zipfile ) )
100 throw FCollException(
"Unable to find zip structure: End-of-central-directory" ) ;
103 _vs.vseekg( _zipfile, _eocd.offset(), ios::beg ) ;
107 _entries.resize ( _eocd.totalCount(), 0 ) ;
108 while ( ( entry_num < _eocd.totalCount() ) ) {
109 ZipCDirEntry *ent =
new ZipCDirEntry ;
110 _entries[ entry_num ] = ent ;
113 if ( _zipfile.bad() )
114 throw IOException(
"Error reading zip file while reading zip file central directory" ) ;
115 else if ( _zipfile.fail() )
116 throw FCollException(
"Zip file consistency problem. Failure while reading zip file central directory" ) ;
117 else if ( _zipfile.eof() )
118 throw IOException(
"Premature end of file while reading zip file central directory" ) ;
125 int pos = _vs.vtellg( _zipfile ) ;
126 _vs.vseekg( _zipfile, 0, ios::end ) ;
127 int remaining =
static_cast< int >( _vs.vtellg( _zipfile ) ) - pos ;
128 if ( remaining != _eocd.eocdOffSetFromEnd() )
129 throw FCollException(
"Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
133 if ( ! confirmLocalHeaders( _zipfile ) )
134 throw FCollException(
"Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
140bool ZipFile::readEndOfCentralDirectory ( istream &_zipfile ) {
141 BackBuffer bb( _zipfile, _vs ) ;
146 if ( ! bb.readChunk ( read_p ) ) {
150 if ( _eocd.read( bb, read_p ) ) {
160bool ZipFile::confirmLocalHeaders( istream &_zipfile ) {
161 Entries::const_iterator it ;
163 int inconsistencies = 0 ;
165 for ( it = _entries.begin() ; it != _entries.end() ; it++ ) {
166 ent =
static_cast< ZipCDirEntry *
>( (*it).get() ) ;
167 _vs.vseekg( _zipfile, ent->getLocalHeaderOffset(), ios::beg ) ;
169 if ( ! _zipfile || zlh != *ent ) {
174 return ! inconsistencies ;
177void ZipFile::setError (
string error_str ) {
179#ifdef _USE_EXCEPTIONS
182 cerr << error_str << endl ;
The header file for BackBuffer.
FileCollection is an abstract baseclass that represents a collection of files.
virtual ConstEntryPointer getEntry(const string &name, MatchPath matchpath=MATCH) const
Returns a ConstEntryPointer to a FileEntry object for the entry with the specified name.
An object member function may throw this exception, if the operation it normally performs is inapprop...
SimpleSmartPointer is a simple reference counting smart pointer template.
Specialization of ZipLocalEntry, that add fields for storing the extra information,...
ZipFile is a FileCollection, where the files are stored in a .zip file.
virtual ~ZipFile()
Destructor.
virtual void close()
Closes the FileCollection.
ZipFile()
Default constructor.
static ZipFile openEmbeddedZipFile(const string &name)
Opens a Zip archive embedded in another file, by writing the zip archive to the end of the file follo...
virtual FileCollection * clone() const
Create a heap allocated clone of the object this method is called for.
virtual istream * getInputStream(const ConstEntryPointer &entry)
Returns a pointer to an opened istream for the specified FileEntry.
Header file that defines FileCollection.
Header file that defines ZipFile.
Header file that defines some simple data types.