BESUncompressGZ.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <zlib.h>
00034
00035 #include <cstring>
00036 #include <cerrno>
00037 #include <cstdio>
00038 #include <sstream>
00039
00040 using std::ostringstream ;
00041 using std::fopen ;
00042
00043 #include "BESUncompressGZ.h"
00044 #include "BESInternalError.h"
00045 #include "BESDebug.h"
00046
00047 #define CHUNK 4096
00048
00054 void
00055 BESUncompressGZ::uncompress( const string &src, const string &target )
00056 {
00057
00058 char in[CHUNK] ;
00059
00060
00061
00062
00063 gzFile gsrc = gzopen( src.c_str(), "rb" ) ;
00064 if( gsrc == NULL )
00065 {
00066 string err = "Could not open the compressed file " + src ;
00067 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00068 }
00069
00070 FILE *dest = fopen( target.c_str(), "wb" ) ;
00071 if( !dest )
00072 {
00073 char *serr = strerror( errno ) ;
00074 string err = "Unable to create the uncompressed file "
00075 + target + ": " ;
00076 if( serr )
00077 {
00078 err.append( serr ) ;
00079 }
00080 else
00081 {
00082 err.append( "unknown error occurred" ) ;
00083 }
00084 gzclose( gsrc ) ;
00085 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00086 }
00087
00088
00089
00090 bool done = false ;
00091 while( !done )
00092 {
00093 int bytes_read = gzread( gsrc, in, CHUNK ) ;
00094 if( bytes_read == 0 )
00095 {
00096 done = true ;
00097 }
00098 else
00099 {
00100 int bytes_written = fwrite( in, 1, bytes_read, dest) ;
00101 if( bytes_written < bytes_read )
00102 {
00103 ostringstream strm ;
00104 strm << "Error writing uncompressed data "
00105 << "to dest file " << target << ": "
00106 << "wrote " << bytes_written << " "
00107 << "instead of " << bytes_read ;
00108 gzclose( gsrc ) ;
00109 fclose( dest ) ;
00110 throw BESInternalError( strm.str(), __FILE__, __LINE__ ) ;
00111 }
00112 }
00113 }
00114
00115 gzclose( gsrc ) ;
00116 fclose( dest ) ;
00117 }
00118