BESUncompressGZ.cc

Go to the documentation of this file.
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,2005 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 <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     // buffer to hold the uncompressed data
00056     char in[CHUNK] ;
00057 
00058     // open the file to be read by gzopen. If the file is not compressed
00059     // using gzip then all this function will do is trasnfer the data to the
00060     // destination file.
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     // gzread will read the data in uncompressed. All we have to do is write
00087     // it to the destination file.
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 

Generated on Tue Mar 4 23:13:36 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1