OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
BESUncompressGZ.cc
Go to the documentation of this file.
1 // BESUncompressGZ.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <zlib.h>
34 
35 #include <cstdio>
36 #include <cstring>
37 #include <cerrno>
38 #include <sstream>
39 
40 using std::ostringstream ;
41 
42 #include "BESUncompressGZ.h"
43 #include "BESInternalError.h"
44 #include "BESDebug.h"
45 
46 #define CHUNK 4096
47 
53 void
54 BESUncompressGZ::uncompress( const string &src, const string &target )
55 {
56  // buffer to hold the uncompressed data
57  char in[CHUNK] ;
58 
59  // open the file to be read by gzopen. If the file is not compressed
60  // using gzip then all this function will do is trasnfer the data to the
61  // destination file.
62  gzFile gsrc = gzopen( src.c_str(), "rb" ) ;
63  if( gsrc == NULL )
64  {
65  string err = "Could not open the compressed file " + src ;
66  throw BESInternalError( err, __FILE__, __LINE__ ) ;
67  }
68 
69  FILE *dest = fopen( target.c_str(), "wb" ) ;
70  if( !dest )
71  {
72  char *serr = strerror( errno ) ;
73  string err = "Unable to create the uncompressed file "
74  + target + ": " ;
75  if( serr )
76  {
77  err.append( serr ) ;
78  }
79  else
80  {
81  err.append( "unknown error occurred" ) ;
82  }
83  gzclose( gsrc ) ;
84  throw BESInternalError( err, __FILE__, __LINE__ ) ;
85  }
86 
87  // gzread will read the data in uncompressed. All we have to do is write
88  // it to the destination file.
89  bool done = false ;
90  while( !done )
91  {
92  int bytes_read = gzread( gsrc, in, CHUNK ) ;
93  if( bytes_read == 0 )
94  {
95  done = true ;
96  }
97  else
98  {
99  int bytes_written = fwrite( in, 1, bytes_read, dest) ;
100  if( bytes_written < bytes_read )
101  {
102  ostringstream strm ;
103  strm << "Error writing uncompressed data "
104  << "to dest file " << target << ": "
105  << "wrote " << bytes_written << " "
106  << "instead of " << bytes_read ;
107  gzclose( gsrc ) ;
108  fclose( dest ) ;
109  remove( target.c_str() ) ;
110  throw BESInternalError( strm.str(), __FILE__, __LINE__ ) ;
111  }
112  }
113  }
114 
115  gzclose( gsrc ) ;
116  fclose( dest ) ;
117 }
118