bes  Updated for version 3.17.0
BESUncompress3GZ.cc
1 // BESUncompress3GZ.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "BESUncompress3GZ.h"
43 #include "BESInternalError.h"
44 // #include "BESDebug.h"
45 
46 #define CHUNK 4096
47 
55 void BESUncompress3GZ::uncompress(const string &src, int dest_fd)
56 {
57  // buffer to hold the uncompressed data
58  char in[CHUNK];
59 
60  // open the file to be read by gzopen. If the file is not compressed
61  // using gzip then all this function will do is trasnfer the data to the
62  // destination file.
63  gzFile gsrc = gzopen(src.c_str(), "rb");
64  if (gsrc == NULL) {
65  string err = "Could not open the compressed file " + src;
66  throw BESInternalError(err, __FILE__, __LINE__);
67  }
68 
69  // gzread will read the data in uncompressed. All we have to do is write
70  // it to the destination file.
71  bool done = false;
72  while (!done) {
73  int bytes_read = gzread(gsrc, in, CHUNK);
74  if (bytes_read == 0) {
75  done = true;
76  }
77  else {
78  int bytes_written = write(dest_fd, in, bytes_read);
79  if (bytes_written < bytes_read) {
80  ostringstream strm;
81  strm << "Error writing uncompressed data for file " << gsrc << ": wrote "
82  << bytes_written << " instead of " << bytes_read;
83  gzclose(gsrc);
84 
85  throw BESInternalError(strm.str(), __FILE__, __LINE__);
86  }
87  }
88  }
89 
90  gzclose(gsrc);
91 }
92 
exception thrown if inernal error encountered
static void uncompress(const string &src, int dest_fd)
uncompress a file with the .gz file extension