2
#include "zipios-config.h"
4
#include "meta-iostreams.h"
8
#include "zipinputstream.h"
9
#include "zipios_defs.h"
11
#include "backbuffer.h"
19
ZipFile ZipFile::openEmbeddedZipFile( const string &name ) {
20
// open zipfile, read 4 last bytes close file
21
// create ZipFile object.
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 ) ;
30
ZipFile::ZipFile( const string &name , int s_off, int e_off
31
/* , ios::open_mode mode */ )
32
: _vs( s_off, e_off ) {
36
ifstream _zipfile( name.c_str(), ios::in | ios::binary ) ;
41
FileCollection *ZipFile::clone() const {
42
return new ZipFile( *this ) ;
50
void ZipFile::close() {
55
istream *ZipFile::getInputStream( const ConstEntryPointer &entry ) {
57
throw InvalidStateException( "Attempt to use an invalid FileCollection" ) ;
58
return getInputStream( entry->getName() ) ;
61
istream *ZipFile::getInputStream( const string &entry_name,
62
MatchPath matchpath ) {
64
throw InvalidStateException( "Attempt to use an invalid ZipFile" ) ;
66
ConstEntryPointer ent = getEntry( entry_name, matchpath ) ;
71
return new ZipInputStream( _filename,
72
static_cast< const ZipCDirEntry * >( ent.get() )->
73
getLocalHeaderOffset() + _vs.startOffset() ) ;
81
bool ZipFile::init( istream &_zipfile ) {
83
// Check stream error state
85
setError ( "Error reading from file" ) ;
89
_valid = readCentralDirectory( _zipfile ) ;
95
bool ZipFile::readCentralDirectory ( istream &_zipfile ) {
96
// Find and read eocd.
97
if ( ! readEndOfCentralDirectory( _zipfile ) )
98
throw FCollException( "Unable to find zip structure: End-of-central-directory" ) ;
100
// Position read pointer to start of first entry in central dir.
101
_vs.vseekg( _zipfile, _eocd.offset(), ios::beg ) ;
104
// Giving the default argument in the next line to keep Visual C++ quiet
105
_entries.resize ( _eocd.totalCount(), 0 ) ;
106
while ( ( entry_num < _eocd.totalCount() ) ) {
107
ZipCDirEntry *ent = new ZipCDirEntry ;
108
_entries[ entry_num ] = ent ;
111
if ( _zipfile.bad() )
112
throw IOException( "Error reading zip file while reading zip file central directory" ) ;
113
else if ( _zipfile.fail() )
114
throw FCollException( "Zip file consistency problem. Failure while reading zip file central directory" ) ;
115
else if ( _zipfile.eof() )
116
throw IOException( "Premature end of file while reading zip file central directory" ) ;
121
// Consistency check. eocd should start here
123
int pos = _vs.vtellg( _zipfile ) ;
124
_vs.vseekg( _zipfile, 0, ios::end ) ;
125
int remaining = static_cast< int >( _vs.vtellg( _zipfile ) ) - pos ;
126
if ( remaining != _eocd.eocdOffSetFromEnd() )
127
throw FCollException( "Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
129
// Consistency check 2, are local headers consistent with
131
if ( ! confirmLocalHeaders( _zipfile ) )
132
throw FCollException( "Zip file consistency problem. Zip file data fields are inconsistent with zip file layout" ) ;
138
bool ZipFile::readEndOfCentralDirectory ( istream &_zipfile ) {
139
BackBuffer bb( _zipfile, _vs ) ;
144
if ( ! bb.readChunk ( read_p ) ) {
148
if ( _eocd.read( bb, read_p ) ) {
158
bool ZipFile::confirmLocalHeaders( istream &_zipfile ) {
159
Entries::const_iterator it ;
161
int inconsistencies = 0 ;
163
for ( it = _entries.begin() ; it != _entries.end() ; it++ ) {
164
ent = static_cast< ZipCDirEntry * >( (*it).get() ) ;
165
_vs.vseekg( _zipfile, ent->getLocalHeaderOffset(), ios::beg ) ;
167
if ( ! _zipfile || zlh != *ent ) {
172
return ! inconsistencies ;
175
void ZipFile::setError ( string error_str ) {
177
#ifdef _USE_EXCEPTIONS
178
throw error_str ; // define exception class instead.
180
cerr << error_str << endl ; // define operator<< for exception class if such a class replaces string
188
The implementation of ZipFile.
192
Zipios++ - a small C++ library that provides easy access to .zip files.
193
Copyright (C) 2000 Thomas S�ndergaard
195
This library is free software; you can redistribute it and/or
196
modify it under the terms of the GNU Lesser General Public
197
License as published by the Free Software Foundation; either
198
version 2 of the License, or (at your option) any later version.
200
This library is distributed in the hope that it will be useful,
201
but WITHOUT ANY WARRANTY; without even the implied warranty of
202
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
203
Lesser General Public License for more details.
205
You should have received a copy of the GNU Lesser General Public
206
License along with this library; if not, write to the Free Software
207
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA