Zipios++
ziphead.h
Go to the documentation of this file.
1 #ifndef ZIPHEAD_H
2 #define ZIPHEAD_H
3 
4 #include "zipios++/zipios-config.h"
5 
6 #include "zipios++/meta-iostreams.h"
7 #include <string>
8 #include <vector>
9 
10 #include "zipios++/fileentry.h"
11 #include "zipios++/zipios_defs.h"
12 
13 namespace zipios {
14 
15 using std::streampos ;
16 
17 class ZipCDirEntry ;
18 
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 ) ;
26 public:
27  inline ZipLocalEntry( const string &_filename = "",
28  const vector< unsigned char > &_extra_field =
29  vector< unsigned char >() )
30  : gp_bitfield( 0 ),
31  _valid( false ) {
32  setDefaultExtract() ;
33  setName( _filename ) ;
34  setExtra( _extra_field ) ;
35  }
36 
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 ;
49 
50  virtual bool isDirectory() const ;
51 
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 ) ;
60 
61  virtual string toString() const ;
62 
63  int getLocalHeaderSize() const ;
64 
65  bool trailingDataDescriptor() const ;
66 
67  virtual FileEntry *clone() const ;
68 
69  virtual ~ZipLocalEntry() {}
70 protected:
71  static const uint32 signature ;
72  uint16 extract_version ;
73  uint16 gp_bitfield ;
74  uint16 compress_method ;
75  uint16 last_mod_ftime ;
76  uint16 last_mod_fdate ;
77  uint32 crc_32 ;
78  uint32 compress_size ;
79  uint32 uncompress_size ;
80  uint16 filename_len ;
81  uint16 extra_field_len ;
82 
83  string filename ;
84  vector< unsigned char > extra_field ;
85 
86  bool _valid ;
87 };
88 
94  uint32 crc_32 ;
95  uint32 compress_size ;
96  uint32 uncompress_size ;
97 };
98 
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 ) ;
106 public:
107 
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
119  {
120  setComment( _file_comment ) ;
121  setDefaultWriter() ;
122  }
123 
124  void setDefaultWriter() ;
125 
126  inline ZipCDirEntry &operator=( const class ZipCDirEntry &src ) ;
127  virtual string toString() const ;
128 
129  virtual string getComment() const ;
130 
131  virtual void setComment( const string &comment ) ;
132 
133  virtual uint32 getLocalHeaderOffset() const ;
134  virtual void setLocalHeaderOffset( uint32 offset ) ;
135 
136  int getCDirHeaderSize() const ;
137 
138  virtual FileEntry *clone() const ;
139 
140  virtual ~ZipCDirEntry() {}
141 private:
142  static const uint32 signature ;
143  uint16 writer_version ;
144 
145  uint16 file_comment_len ;
146  uint16 disk_num_start ;
147  uint16 intern_file_attr ;
148  uint32 extern_file_attr ;
149 
150  uint32 rel_offset_loc_head ;
151 
152  string file_comment ;
153 };
154 
160  friend ostream &operator<< ( ostream &os, const EndOfCentralDirectory &eocd ) ;
161 public:
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 ( _zip_comment.size() ),
174  zip_comment ( _zip_comment ) {}
175 
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 ; }
180 
181  void setTotalCount( uint16 c ) { cdir_entries = c ; cdir_tot_entries = c ; }
182  int eocdOffSetFromEnd() const { return eocd_offset_from_end ; }
183  bool read( vector<unsigned char> &buf, int pos ) ;
184 private:
185  static const uint32 signature;
186  uint16 disk_num ;
187  uint16 cdir_disk_num ;
188  uint16 cdir_entries ;
189  uint16 cdir_tot_entries ;
190  uint32 cdir_size ;
191  uint32 cdir_offset ;
192  uint16 zip_comment_len ;
193 
194  streampos eocd_offset_from_end ; // Not a Zip defined field
195  string zip_comment;
196  bool checkSignature( unsigned char *buf ) const ;
197  inline bool checkSignature( uint32 sig ) const ;
198 };
199 
200 
201 bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) ;
202 inline bool operator== ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
203  return zlh == ze ;
204 }
205 inline bool operator!= ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
206  return ! ( zlh == ze ) ;
207 }
208 inline bool operator!= ( const ZipCDirEntry &ze, const ZipLocalEntry &zlh ) {
209  return ! ( zlh == ze ) ;
210 }
211 
212 // Inline member functions
213 
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 ;
231 
232  filename = src.filename ;
233  extra_field = src.extra_field ;
234  file_comment = src.file_comment ;
235 
236  return *this ;
237 }
238 
239 bool EndOfCentralDirectory::checkSignature ( uint32 sig ) const {
240  return signature == sig ;
241 }
242 
243 
244 } // namespace
245 
246 #endif
247 
248 
254 /*
255  Zipios++ - a small C++ library that provides easy access to .zip files.
256  Copyright (C) 2000 Thomas Søndergaard
257 
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.
262 
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.
267 
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
271 */
virtual string getComment() const
Returns the comment of the entry, if it has one.
Definition: ziphead.cpp:199
virtual bool isValid() const
Any method or operator that initializes a FileEntry may set a flag, that specifies whether the read e...
Definition: ziphead.cpp:108
virtual void setTime(int time)
Sets the time field for the entry.
Definition: ziphead.cpp:148
virtual void setCompressedSize(uint32 size)
Set the compressed size field of the entry.
Definition: ziphead.cpp:122
virtual uint32 getCrc() const
Returns the Crc for the entry, if it has one.
Definition: ziphead.cpp:70
virtual uint32 getCompressedSize() const
Returns the compressed size of the entry.
Definition: ziphead.cpp:66
Header file that defines some simple data types.
The end of the Central directory structure.
Definition: ziphead.h:159
Header file that defines FileEntry.
virtual string getComment() const
Returns the comment of the entry, if it has one.
Definition: ziphead.cpp:62
virtual string getFileName() const
Returns the filename of the entry.
Definition: ziphead.cpp:86
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
Definition: ziphead.cpp:135
virtual int getTime() const
Returns the date and time of FIXME: what?
Definition: ziphead.cpp:103
virtual FileEntry * clone() const
Create a heap allocated clone of the object this method is called for.
Definition: ziphead.cpp:231
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.
Definition: ziphead.h:102
virtual void setSize(uint32 size)
Sets the size field for the entry.
Definition: ziphead.cpp:144
virtual string toString() const
Returns a human-readable string representation of the entry.
Definition: ziphead.cpp:157
virtual bool isDirectory() const
Returns true if the entry is a directory.
Definition: ziphead.cpp:112
virtual void setCrc(uint32 crc)
Sets the crc field.
Definition: ziphead.cpp:126
virtual void setComment(const string &comment)
Sets the comment field for the FileEntry.
Definition: ziphead.cpp:118
virtual uint32 getSize() const
Returns the (uncompressed) size of the entry data.
Definition: ziphead.cpp:99
virtual void setName(const string &name)
Sets the name field for the entry.
Definition: ziphead.cpp:139
virtual vector< unsigned char > getExtra() const
Returns a vector of bytes of extra data that may be stored with the entry.
Definition: ziphead.cpp:74
A struct containing fields for the entries in a zip file data descriptor, that trails the compressed ...
Definition: ziphead.h:93
virtual string getName() const
Returns the full filename of the entry, including a path if the entry is stored in a subfolder...
Definition: ziphead.cpp:82
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.
Definition: ziphead.h:22
virtual void setExtra(const vector< unsigned char > &extra)
Sets the extra field.
Definition: ziphead.cpp:130
virtual void setComment(const string &comment)
Sets the comment field for the FileEntry.
Definition: ziphead.cpp:212
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.h:52
virtual string toString() const
Returns a human-readable string representation of the entry.
Definition: ziphead.cpp:218
virtual FileEntry * clone() const
Create a heap allocated clone of the object this method is called for.
Definition: ziphead.cpp:178
virtual StorageMethod getMethod() const
Returns the method used to store the entry in the FileCollection.
Definition: ziphead.cpp:78