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