ziphead.cpp
Go to the documentation of this file.
#include "zipios++/zipios-config.h"
#include "zipios++/meta-iostreams.h"
#include <iterator>
#include <string>
#include <cassert>
#include "zipios_common.h"
#include "zipios++/ziphead.h"
#include "zipios++/zipheadio.h"
#include "zipios++/zipios_defs.h"
#include "outputstringstream.h"
namespace zipios {
using std::ios ;
bool operator== ( const ZipLocalEntry &zlh, const ZipCDirEntry &ze ) {
// Not all fields need to be identical. Some of the information
// may be put in a data descriptor that trails the compressed
// data, according to the specs (The trailing data descriptor
// can contain crc_32, compress_size and uncompress_size.)
// Experience has shown that extra_field and extra_field_len
// can differ too.
// cerr << "----- BEGIN -----" << endl ;
// cerr << ( zlh.extract_version == ze.extract_version ) << endl ;
// cerr << ( zlh.gp_bitfield == ze.gp_bitfield ) << endl ;
// cerr << ( zlh.compress_method == ze.compress_method ) << endl ;
// cerr << ( zlh.last_mod_ftime == ze.last_mod_ftime ) << endl ;
// cerr << ( zlh.last_mod_fdate == ze.last_mod_fdate ) << endl ;
// cerr << ( zlh.filename_len == ze.filename_len ) << endl ;
// cerr << ( zlh.filename == ze.filename ) << endl ;
// cerr << "----- END -----" << endl ;
return ( zlh.extract_version == ze.extract_version &&
zlh.gp_bitfield == ze.gp_bitfield &&
zlh.compress_method == ze.compress_method &&
zlh.last_mod_ftime == ze.last_mod_ftime &&
zlh.last_mod_fdate == ze.last_mod_fdate &&
zlh.filename_len == ze.filename_len &&
zlh.filename == ze.filename ) ;
}
//
// ZipLocalEntry methods
//
const uint32 ZipLocalEntry::signature = 0x04034b50 ;
void ZipLocalEntry::setDefaultExtract() {
extract_version = 20 ; // version number
}
string ZipLocalEntry::getComment() const {
return "" ; // No comment in a local entry
}
uint32 ZipLocalEntry::getCompressedSize() const {
return compress_size ;
}
uint32 ZipLocalEntry::getCrc() const {
return crc_32 ;
}
vector< unsigned char > ZipLocalEntry::getExtra() const {
return extra_field ;
}
StorageMethod ZipLocalEntry::getMethod() const {
return static_cast< StorageMethod >( compress_method ) ;
}
string ZipLocalEntry::getName() const {
return filename ;
}
string ZipLocalEntry::getFileName() const {
if ( isDirectory() )
return string() ;
string::size_type pos ;
pos = filename.find_last_of( separator ) ;
if ( pos != string::npos ) { // separator found!
// isDirectory() check means pos should not be last, so pos+1 is ok
return filename.substr( pos + 1 ) ;
} else {
return filename ;
}
}
uint32 ZipLocalEntry::getSize() const {
return uncompress_size ;
}
int ZipLocalEntry::getTime() const {
return ( last_mod_fdate << 16 ) + last_mod_ftime ;
// FIXME: what to do with this time date thing? (not only here?)
}
bool ZipLocalEntry::isValid() const {
return _valid ;
}
bool ZipLocalEntry::isDirectory() const {
assert( filename.size() != 0 ) ;
return filename[ filename.size() - 1 ] == separator ;
}
void ZipLocalEntry::setComment( const string & ) {
// A local entry cannot hold a comment
}
void ZipLocalEntry::setCompressedSize( uint32 size ) {
compress_size = size ;
}
void ZipLocalEntry::setCrc( uint32 crc ) {
crc_32 = crc ;
}
void ZipLocalEntry::setExtra( const vector< unsigned char > &extra ) {
extra_field = extra ;
extra_field_len = extra_field.size() ;
}
void ZipLocalEntry::setMethod( StorageMethod method ) {
compress_method = static_cast< uint16 >( method ) ;
}
void ZipLocalEntry::setName( const string &name ) {
filename = name ;
filename_len = filename.size() ;
}
void ZipLocalEntry::setSize( uint32 size ) {
uncompress_size = size ;
}
void ZipLocalEntry::setTime( int time ) {
// FIXME: fix time setting here, and ind flist and elsewhere. Define the
// date time semantics before mucking about - how surprising
// Mark Donszelmann: added these lines to make zip work for winzip
last_mod_fdate = (time >> 16) & 0x0000FFFF;
last_mod_ftime = time & 0x0000FFFF;
}
string ZipLocalEntry::toString() const {
OutputStringStream sout ;
sout << filename << " (" << uncompress_size << " bytes, " ;
sout << compress_size << " bytes compressed)" ;
return sout.str() ;
}
int ZipLocalEntry::getLocalHeaderSize() const {
return 30 + filename.size() + extra_field.size() ;
}
bool ZipLocalEntry::trailingDataDescriptor() const {
// gp_bitfield bit 3 is one, if this entry uses a trailing data
// descriptor to keep size, compressed size and crc-32
// fields.
if ( ( gp_bitfield & 4 ) == 1 )
return true ;
else
return false ;
}
FileEntry *ZipLocalEntry::clone() const {
return new ZipLocalEntry( *this ) ;
}
//
// ZipCDirEntry methods
//
const uint32 ZipCDirEntry::signature = 0x02014b50 ;
void ZipCDirEntry::setDefaultWriter() {
writer_version = 0 ;
#ifdef WIN32
writer_version |= static_cast< uint16 >( 0 ) << 8 ; // Windows, DOS
#else
writer_version |= static_cast< uint16 >( 3 ) << 8 ; // Unix
#endif
writer_version |= 20 ; // version number
}
string ZipCDirEntry::getComment() const {
return file_comment ;
}
uint32 ZipCDirEntry::getLocalHeaderOffset() const {
return rel_offset_loc_head ;
}
void ZipCDirEntry::setLocalHeaderOffset( uint32 offset ) {
rel_offset_loc_head = offset ;
}
void ZipCDirEntry::setComment( const string &comment ) {
file_comment = comment ;
file_comment_len = file_comment.size() ;
}
string ZipCDirEntry::toString() const {
OutputStringStream sout ;
sout << filename << " (" << uncompress_size << " bytes, " ;
sout << compress_size << " bytes compressed)" ;
return sout.str() ;
}
int ZipCDirEntry::getCDirHeaderSize() const {
return 46 + filename.size() + extra_field.size() + file_comment.size() ;
}
FileEntry *ZipCDirEntry::clone() const {
return new ZipCDirEntry( *this ) ;
}
//
// EndOfCentralDirectory methods
//
const uint32 EndOfCentralDirectory::signature = 0x06054b50 ;
bool EndOfCentralDirectory::read( vector<unsigned char> &buf, int pos ) {
if ( ( buf.size() - pos < sizeof( uint32 ) ) ||
( ! checkSignature( &( buf[ pos ] ) ) ) )
return false ;
eocd_offset_from_end = buf.size() - pos ;
pos += sizeof( uint32 ) ;
disk_num = ztohs( &( buf[ pos ] ) ) ; pos += sizeof( uint16 ) ;
cdir_disk_num = ztohs( &( buf[ pos ] ) ) ; pos += sizeof( uint16 ) ;
cdir_entries = ztohs( &( buf[ pos ] ) ) ; pos += sizeof( uint16 ) ;
cdir_tot_entries = ztohs( &( buf[ pos ] ) ) ; pos += sizeof( uint16 ) ;
cdir_size = ztohl( &( buf[ pos ] ) ) ; pos += sizeof( uint32 ) ;
cdir_offset = ztohl( &( buf[ pos ] ) ) ; pos += sizeof( uint32 ) ;
zip_comment_len = ztohs( &( buf[ pos ] ) ) ; pos += sizeof( uint16 ) ;
// cerr << "Zip comment length = " << zip_comment_len << endl ;
// cerr << "Length of remaining file = " << buf.size() - pos << endl ;
return true ; // Dummy
}
bool EndOfCentralDirectory::checkSignature ( unsigned char *buf ) const {
// cerr << "potential header: " << ztohl( buf ) << endl ;
return checkSignature( ztohl( buf ) ) ;
}
} // namespace
/*
Zipios++ - a small C++ library that provides easy access to .zip files.
Copyright (C) 2000 Thomas Søndergaard
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
zipios::FileEntry
A FileEntry represents an entry in a FileCollection.
Definition fileentry.h:52
zipios::OutputStringStream
OutputStringStream is typedefed to ostringstream if sstream is part of the standard library (unless Z...
Definition outputstringstream.h:24
zipios::OutputStringStream::str
string str()
Specialization of ostrstream::str() that takes care of null-terminating the string and unfreezing the...
Definition outputstringstream.h:29
zipios::ZipCDirEntry
Specialization of ZipLocalEntry, that add fields for storing the extra information,...
Definition ziphead.h:102
zipios::ZipCDirEntry::clone
virtual FileEntry * clone() const
Create a heap allocated clone of the object this method is called for.
Definition ziphead.cpp:231
zipios::ZipCDirEntry::getComment
virtual string getComment() const
Returns the comment of the entry, if it has one.
Definition ziphead.cpp:199
zipios::ZipCDirEntry::setComment
virtual void setComment(const string &comment)
Sets the comment field for the FileEntry.
Definition ziphead.cpp:212
zipios::ZipCDirEntry::toString
virtual string toString() const
Returns a human-readable string representation of the entry.
Definition ziphead.cpp:218
zipios::ZipLocalEntry
A concrete implementation of the abstract FileEntry base class for ZipFile entries,...
Definition ziphead.h:22
zipios::ZipLocalEntry::getMethod
virtual StorageMethod getMethod() const
Returns the method used to store the entry in the FileCollection.
Definition ziphead.cpp:78
zipios::ZipLocalEntry::getExtra
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
zipios::ZipLocalEntry::setExtra
virtual void setExtra(const vector< unsigned char > &extra)
Sets the extra field.
Definition ziphead.cpp:130
zipios::ZipLocalEntry::getTime
virtual int getTime() const
Returns the date and time of FIXME: what?
Definition ziphead.cpp:103
zipios::ZipLocalEntry::getFileName
virtual string getFileName() const
Returns the filename of the entry.
Definition ziphead.cpp:86
zipios::ZipLocalEntry::setComment
virtual void setComment(const string &comment)
Sets the comment field for the FileEntry.
Definition ziphead.cpp:118
zipios::ZipLocalEntry::isValid
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
zipios::ZipLocalEntry::setSize
virtual void setSize(uint32 size)
Sets the size field for the entry.
Definition ziphead.cpp:144
zipios::ZipLocalEntry::setTime
virtual void setTime(int time)
Sets the time field for the entry.
Definition ziphead.cpp:148
zipios::ZipLocalEntry::getCompressedSize
virtual uint32 getCompressedSize() const
Returns the compressed size of the entry.
Definition ziphead.cpp:66
zipios::ZipLocalEntry::setMethod
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
Definition ziphead.cpp:135
zipios::ZipLocalEntry::getComment
virtual string getComment() const
Returns the comment of the entry, if it has one.
Definition ziphead.cpp:62
zipios::ZipLocalEntry::getName
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
zipios::ZipLocalEntry::toString
virtual string toString() const
Returns a human-readable string representation of the entry.
Definition ziphead.cpp:157
zipios::ZipLocalEntry::clone
virtual FileEntry * clone() const
Create a heap allocated clone of the object this method is called for.
Definition ziphead.cpp:178
zipios::ZipLocalEntry::getCrc
virtual uint32 getCrc() const
Returns the Crc for the entry, if it has one.
Definition ziphead.cpp:70
zipios::ZipLocalEntry::isDirectory
virtual bool isDirectory() const
Returns true if the entry is a directory.
Definition ziphead.cpp:112
zipios::ZipLocalEntry::setCrc
virtual void setCrc(uint32 crc)
Sets the crc field.
Definition ziphead.cpp:126
zipios::ZipLocalEntry::setCompressedSize
virtual void setCompressedSize(uint32 size)
Set the compressed size field of the entry.
Definition ziphead.cpp:122
zipios::ZipLocalEntry::getSize
virtual uint32 getSize() const
Returns the (uncompressed) size of the entry data.
Definition ziphead.cpp:99
zipios::ZipLocalEntry::setName
virtual void setName(const string &name)
Sets the name field for the entry.
Definition ziphead.cpp:139
zipios::StorageMethod
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition fileentry.h:25
outputstringstream.h
Header file that defines OutputStringStream.
ziphead.h
Header file containing classes and functions for reading the central directory and local header field...
zipheadio.h
Header file that defines I/O functions for the header structures defined in ziphead....
zipios_common.h
Header file containing miscellaneous small functions.
zipios_defs.h
Header file that defines some simple data types.