gzipoutputstreambuf.cpp
Go to the documentation of this file.00001
00002 #include <time.h>
00003
00004 #include "zipios-config.h"
00005
00006 #include <algorithm>
00007 #include "meta-iostreams.h"
00008
00009 #include <zlib.h>
00010
00011 #include "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;
00070 os << (unsigned char)0x8b;
00071 os << (unsigned char)0x08;
00072 os << flg;
00073 os << (unsigned char)0x00;
00074 os << (unsigned char)0x00;
00075 os << (unsigned char)0x00;
00076 os << (unsigned char)0x00;
00077 os << (unsigned char)0x00;
00078 os << (unsigned char)0x00;
00079
00080 if (_filename != "") {
00081 os << _filename.c_str();
00082 os << (unsigned char)0x00;
00083 }
00084
00085 if (_comment != "") {
00086 os << _comment.c_str();
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 }
00105
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127