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 Atmostpheric 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 <stdlib.h>
00034 #include <stdio.h>
00035 #include <errno.h>
00036 #include <zlib.h>
00037 
00038 #include <sstream>
00039 
00040 using std::ostringstream ;
00041 
00042 #include "BESUncompressGZ.h"
00043 #include "BESContainerStorageException.h"
00044 #include "BESDebug.h"
00045 
00046 #define CHUNK 4096
00047 
00054 string
00055 BESUncompressGZ::uncompress( const string &src, const string &target )
00056 {
00057     // buffer to hold the uncompressed data
00058     char in[CHUNK] ;
00059 
00060     // open the file to be read by gzopen. If the file is not compressed
00061     // using gzip then all this function will do is trasnfer the data to the
00062     // destination file.
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 BESContainerStorageException( 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 BESContainerStorageException( err, __FILE__, __LINE__ ) ;
00086     }
00087 
00088     // gzread will read the data in uncompressed. All we have to do is write
00089     // it to the destination file.
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 BESContainerStorageException( strm.str(), __FILE__, __LINE__ ) ;
00111             }
00112         }
00113     }
00114 
00115     gzclose( gsrc ) ;
00116     fclose( dest ) ;
00117 
00118     return target ;
00119 }
00120 

Generated on Wed Aug 29 03:24:04 2007 for OPeNDAP Back End Server (BES) by  doxygen 1.5.2