4 #include "zipios++/zipios-config.h"
6 #include "zipios++/meta-iostreams.h"
10 #include "zipios++/fileentry.h"
11 #include "zipios++/zipios_defs.h"
15 using std::streampos ;
19 /** A concrete implementation of the abstract FileEntry base class for
20 ZipFile entries, specifically for representing the information
21 present in the local headers of file entries in a zip file. */
22 class ZipLocalEntry : public FileEntry {
23 friend istream &operator>> ( istream &is, ZipLocalEntry &zcdh ) ;
24 friend ostream &operator<< ( ostream &os, const ZipLocalEntry &zlh ) ;
25 friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
27 inline ZipLocalEntry( const string &_filename = "",
28 const vector< unsigned char > &_extra_field =
29 vector< unsigned char >() )
33 setName( _filename ) ;
34 setExtra( _extra_field ) ;
37 void setDefaultExtract() ;
38 inline ZipLocalEntry &operator=( const class ZipLocalEntry &src ) ;
39 virtual string getComment() const ;
40 virtual uint32 getCompressedSize() const ;
41 virtual uint32 getCrc() const ;
42 virtual vector< unsigned char > getExtra() const ;
43 virtual StorageMethod getMethod() const ;
44 virtual string getName() const ;
45 virtual string getFileName() const ;
46 virtual uint32 getSize() const ;
47 virtual int getTime() const ;
48 virtual bool isValid() const ;
50 virtual bool isDirectory() const ;
52 virtual void setComment( const string &comment ) ;
53 virtual void setCompressedSize( uint32 size ) ;
54 virtual void setCrc( uint32 crc ) ;
55 virtual void setExtra( const vector< unsigned char > &extra ) ;
56 virtual void setMethod( StorageMethod method ) ;
57 virtual void setName( const string &name ) ;
58 virtual void setSize( uint32 size ) ;
59 virtual void setTime( int time ) ;
61 virtual string toString() const ;
63 int getLocalHeaderSize() const ;
65 bool trailingDataDescriptor() const ;
67 virtual FileEntry *clone() const ;
69 virtual ~ZipLocalEntry() {}
71 static const uint32 signature ;
72 uint16 extract_version ;
74 uint16 compress_method ;
75 uint16 last_mod_ftime ;
76 uint16 last_mod_fdate ;
78 uint32 compress_size ;
79 uint32 uncompress_size ;
81 uint16 extra_field_len ;
84 vector< unsigned char > extra_field ;
89 /** A struct containing fields for the entries in a zip file data
90 descriptor, that trails the compressed data in files that were
91 created by streaming, ie where the zip compressor cannot seek back
92 to the local header and store the data. */
93 struct DataDescriptor {
95 uint32 compress_size ;
96 uint32 uncompress_size ;
99 /** Specialization of ZipLocalEntry, that add fields for storing the
100 extra information, that is only present in the entries in the zip
101 central directory and not in the local entry headers. */
102 class ZipCDirEntry : public ZipLocalEntry {
103 friend istream &operator>> ( istream &is, ZipCDirEntry &zcdh ) ;
104 friend ostream &operator<< ( ostream &os, const ZipCDirEntry &zcdh ) ;
105 friend bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
108 inline ZipCDirEntry( const string &_filename = "",
109 const string &_file_comment = "",
110 const vector< unsigned char > &_extra_field =
111 vector< unsigned char >() )
112 : ZipLocalEntry ( _filename, _extra_field ),
113 disk_num_start ( 0x0 ),
114 intern_file_attr( 0x0 ),
115 extern_file_attr( 0x81B40000 )
116 // FIXME: I don't understand the external mapping, simply
117 // copied value for a file with -rw-rw-r-- permissions
118 // compressed with info-zip
120 setComment( _file_comment ) ;
124 void setDefaultWriter() ;
126 inline ZipCDirEntry &operator=( const class ZipCDirEntry &src ) ;
127 virtual string toString() const ;
129 virtual string getComment() const ;
131 virtual void setComment( const string &comment ) ;
133 virtual uint32 getLocalHeaderOffset() const ;
134 virtual void setLocalHeaderOffset( uint32 offset ) ;
136 int getCDirHeaderSize() const ;
138 virtual FileEntry *clone() const ;
140 virtual ~ZipCDirEntry() {}
142 static const uint32 signature ;
143 uint16 writer_version ;
145 uint16 file_comment_len ;
146 uint16 disk_num_start ;
147 uint16 intern_file_attr ;
148 uint32 extern_file_attr ;
150 uint32 rel_offset_loc_head ;
152 string file_comment ;
155 /** The end of the Central directory structure. This structure is
156 stored in the end of the zipfile, and contains information about
157 the zipfile, including the position of the start of the central
159 class EndOfCentralDirectory {
160 friend ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
162 explicit EndOfCentralDirectory( const string &_zip_comment = "",
163 uint16 _disk_num = 0, uint16 _cdir_disk_num = 0,
164 uint16 _cdir_entries = 0,
165 uint16 _cdir_tot_entries = 0,
166 uint32 _cdir_size = 0, uint32 _cdir_offset = 0 )
167 : disk_num ( _disk_num ),
168 cdir_disk_num ( _cdir_disk_num ),
169 cdir_entries ( _cdir_entries ),
170 cdir_tot_entries ( _cdir_tot_entries ),
171 cdir_size ( _cdir_size ),
172 cdir_offset ( _cdir_offset ),
173 zip_comment_len ( static_cast< uint16 >( _zip_comment.size() ) ),
174 zip_comment ( _zip_comment ) {}
176 uint32 offset() const { return cdir_offset ; }
177 uint16 totalCount() const { return cdir_tot_entries ; }
178 void setCDirSize( uint32 size ) { cdir_size = size ; }
179 void setOffset( uint32 offset ) { cdir_offset = offset ; }
181 void setTotalCount( uint16 c ) { cdir_entries = c ; cdir_tot_entries = c ; }
182 int eocdOffSetFromEnd() const { return static_cast< int >( eocd_offset_from_end ) ; }
183 bool read( vector<unsigned char> &buf, int pos ) ;
185 static const uint32 signature;
187 uint16 cdir_disk_num ;
188 uint16 cdir_entries ;
189 uint16 cdir_tot_entries ;
192 uint16 zip_comment_len ;
194 streampos eocd_offset_from_end ; // Not a Zip defined field
196 bool checkSignature( unsigned char *buf ) const ;
197 inline bool checkSignature( uint32 sig ) const ;
201 bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
202 inline bool operator== ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
205 inline bool operator!= ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
206 return ! ( zlh == ze ) ;
208 inline bool operator!= ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
209 return ! ( zlh == ze ) ;
212 // Inline member functions
214 ZipCDirEntry &ZipCDirEntry::operator=( const class ZipCDirEntry &src ) {
215 writer_version = src.writer_version ;
216 extract_version = src.extract_version ;
217 gp_bitfield = src.gp_bitfield ;
218 compress_method = src.compress_method ;
219 last_mod_ftime = src.last_mod_ftime ;
220 last_mod_fdate = src.last_mod_fdate ;
221 crc_32 = src.crc_32 ;
222 compress_size = src.compress_size ;
223 uncompress_size = src.uncompress_size ;
224 filename_len = src.filename_len ;
225 extra_field_len = src.extra_field_len ;
226 file_comment_len = src.file_comment_len ;
227 disk_num_start = src.disk_num_start ;
228 intern_file_attr = src.intern_file_attr ;
229 extern_file_attr = src.extern_file_attr ;
230 rel_offset_loc_head = src.rel_offset_loc_head ;
232 filename = src.filename ;
233 extra_field = src.extra_field ;
234 file_comment = src.file_comment ;
239 bool EndOfCentralDirectory::checkSignature ( uint32 sig ) const {
240 return signature == sig ;
250 Header file containing classes and functions for reading the central
251 directory and local header fields in a zip archive.
255 Zipios++ - a small C++ library that provides easy access to .zip files.
256 Copyright (C) 2000 Thomas Søndergaard
258 This library is free software; you can redistribute it and/or
259 modify it under the terms of the GNU Lesser General Public
260 License as published by the Free Software Foundation; either
261 version 2 of the License, or (at your option) any later version.
263 This library is distributed in the hope that it will be useful,
264 but WITHOUT ANY WARRANTY; without even the implied warranty of
265 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
266 Lesser General Public License for more details.
268 You should have received a copy of the GNU Lesser General Public
269 License along with this library; if not, write to the Free Software
270 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA