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