Zipios++
|
00001 00002 #include <time.h> 00003 00004 #include "zipios++/zipios-config.h" 00005 00006 #include <algorithm> 00007 #include "zipios++/meta-iostreams.h" 00008 00009 #include <zlib.h> 00010 00011 #include "zipios++/gzipoutputstreambuf.h" 00012 00013 namespace zipios { 00014 00015 using std::ios ; 00016 using std::cerr ; 00017 using std::endl ; 00018 00019 GZIPOutputStreambuf::GZIPOutputStreambuf( streambuf *outbuf, bool del_outbuf ) 00020 : DeflateOutputStreambuf( outbuf, true, del_outbuf ), 00021 _open ( false ) 00022 { 00023 } 00024 00025 void GZIPOutputStreambuf::setFilename( const string &filename ) { 00026 _filename = filename ; 00027 } 00028 00029 void GZIPOutputStreambuf::setComment( const string &comment ) { 00030 _comment = comment ; 00031 } 00032 00033 void GZIPOutputStreambuf::close() { 00034 finish() ; 00035 } 00036 00037 void GZIPOutputStreambuf::finish() { 00038 if( ! _open ) 00039 return ; 00040 00041 closeStream(); 00042 writeTrailer(); 00043 00044 _open = false ; 00045 } 00046 00047 GZIPOutputStreambuf::~GZIPOutputStreambuf() { 00048 finish() ; 00049 } 00050 00051 int GZIPOutputStreambuf::overflow( int c ) { 00052 if (!_open) { 00053 writeHeader(); 00054 _open = true; 00055 } 00056 return DeflateOutputStreambuf::overflow( c ) ; 00057 } 00058 00059 int GZIPOutputStreambuf::sync() { 00060 return DeflateOutputStreambuf::sync() ; 00061 } 00062 00063 void GZIPOutputStreambuf::writeHeader() { 00064 unsigned char flg = 0x00; 00065 flg |= (_filename == "") ? 0x00 : 0x08; 00066 flg |= (_comment == "") ? 0x00 : 0x10; 00067 00068 ostream os( _outbuf ) ; 00069 os << (unsigned char)0x1f; // Magic # 00070 os << (unsigned char)0x8b; // Magic # 00071 os << (unsigned char)0x08; // Deflater.DEFLATED 00072 os << flg; // FLG 00073 os << (unsigned char)0x00; // MTIME 00074 os << (unsigned char)0x00; // MTIME 00075 os << (unsigned char)0x00; // MTIME 00076 os << (unsigned char)0x00; // MTIME 00077 os << (unsigned char)0x00; // XFLG 00078 os << (unsigned char)0x00; // OS 00079 00080 if (_filename != "") { 00081 os << _filename.c_str();// Filename 00082 os << (unsigned char)0x00; 00083 } 00084 00085 if (_comment != "") { 00086 os << _comment.c_str(); // Comment 00087 os << (unsigned char)0x00; 00088 } 00089 } 00090 00091 void GZIPOutputStreambuf::writeTrailer() { 00092 writeInt(getCrc32()); 00093 writeInt(getCount()); 00094 } 00095 00096 void GZIPOutputStreambuf::writeInt(uint32 i) { 00097 ostream os( _outbuf ) ; 00098 os << (unsigned char)( i & 0xFF); 00099 os << (unsigned char)((i >> 8) & 0xFF); 00100 os << (unsigned char)((i >> 16) & 0xFF); 00101 os << (unsigned char)((i >> 24) & 0xFF); 00102 } 00103 00104 } // namespace 00105 00110 /* 00111 Zipios++ - a small C++ library that provides easy access to .zip files. 00112 Copyright (C) 2000 Thomas Søndergaard 00113 00114 This library is free software; you can redistribute it and/or 00115 modify it under the terms of the GNU Lesser General Public 00116 License as published by the Free Software Foundation; either 00117 version 2 of the License, or (at your option) any later version. 00118 00119 This library is distributed in the hope that it will be useful, 00120 but WITHOUT ANY WARRANTY; without even the implied warranty of 00121 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00122 Lesser General Public License for more details. 00123 00124 You should have received a copy of the GNU Lesser General Public 00125 License along with this library; if not, write to the Free Software 00126 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00127 */