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 <sstream>
00038
00039 using std::ostringstream ;
00040
00041 #include "BESUncompressGZ.h"
00042 #include "BESInternalError.h"
00043 #include "BESDebug.h"
00044
00045 #define CHUNK 4096
00046
00052 void
00053 BESUncompressGZ::uncompress( const string &src, const string &target )
00054 {
00055
00056 char in[CHUNK] ;
00057
00058
00059
00060
00061 gzFile gsrc = gzopen( src.c_str(), "rb" ) ;
00062 if( gsrc == NULL )
00063 {
00064 string err = "Could not open the compressed file " + src ;
00065 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00066 }
00067
00068 FILE *dest = fopen( target.c_str(), "wb" ) ;
00069 if( !dest )
00070 {
00071 char *serr = strerror( errno ) ;
00072 string err = "Unable to create the uncompressed file "
00073 + target + ": " ;
00074 if( serr )
00075 {
00076 err.append( serr ) ;
00077 }
00078 else
00079 {
00080 err.append( "unknown error occurred" ) ;
00081 }
00082 gzclose( gsrc ) ;
00083 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00084 }
00085
00086
00087
00088 bool done = false ;
00089 while( !done )
00090 {
00091 int bytes_read = gzread( gsrc, in, CHUNK ) ;
00092 if( bytes_read == 0 )
00093 {
00094 done = true ;
00095 }
00096 else
00097 {
00098 int bytes_written = fwrite( in, 1, bytes_read, dest) ;
00099 if( bytes_written < bytes_read )
00100 {
00101 ostringstream strm ;
00102 strm << "Error writing uncompressed data "
00103 << "to dest file " << target << ": "
00104 << "wrote " << bytes_written << " "
00105 << "instead of " << bytes_read ;
00106 gzclose( gsrc ) ;
00107 fclose( dest ) ;
00108 throw BESInternalError( strm.str(), __FILE__, __LINE__ ) ;
00109 }
00110 }
00111 }
00112
00113 gzclose( gsrc ) ;
00114 fclose( dest ) ;
00115 }
00116