OPeNDAP Hyrax Back End Server (BES) Updated for version 3.8.3
|
00001 // BESUncompressGZ.cc 00002 00003 // This file is part of bes, A C++ back-end server implementation framework 00004 // for the OPeNDAP Data Access Protocol. 00005 00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research 00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu> 00008 // 00009 // This library is free software; you can redistribute it and/or 00010 // modify it under the terms of the GNU Lesser General Public 00011 // License as published by the Free Software Foundation; either 00012 // version 2.1 of the License, or (at your option) any later version. 00013 // 00014 // This library is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 // Lesser General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU Lesser General Public 00020 // License along with this library; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 // 00023 // You can contact University Corporation for Atmospheric Research at 00024 // 3080 Center Green Drive, Boulder, CO 80301 00025 00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005 00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR. 00028 // 00029 // Authors: 00030 // pwest Patrick West <pwest@ucar.edu> 00031 // jgarcia Jose Garcia <jgarcia@ucar.edu> 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 // buffer to hold the uncompressed data 00057 char in[CHUNK] ; 00058 00059 // open the file to be read by gzopen. If the file is not compressed 00060 // using gzip then all this function will do is trasnfer the data to the 00061 // destination file. 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 // gzread will read the data in uncompressed. All we have to do is write 00088 // it to the destination file. 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