Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
gzipstring.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef OR_TOOLS_BASE_GZIPSTRING_H_
15#define OR_TOOLS_BASE_GZIPSTRING_H_
16
17#include <string>
18
19#include "absl/log/log.h"
20#include "absl/strings/string_view.h"
21#include "zconf.h"
22#include "zlib.h"
23
24inline bool GunzipString(absl::string_view str, std::string* out) {
25 z_stream zs;
26 zs.zalloc = Z_NULL;
27 zs.zfree = Z_NULL;
28 zs.opaque = Z_NULL;
29 zs.next_in = Z_NULL;
30 zs.avail_in = 0;
31 zs.next_out = Z_NULL;
32 if (inflateInit2(&zs, /*window_bits=*/15 + 32) != Z_OK) {
33 return false;
34 }
35
36 zs.next_in = (Bytef*)str.data();
37 zs.avail_in = str.size();
38
39 int status;
40 char buffer[32768];
41
42 // Decompress string by block.
43 do {
44 zs.next_out = reinterpret_cast<Bytef*>(buffer);
45 zs.avail_out = sizeof(buffer);
46
47 status = inflate(&zs, 0);
48
49 if (out->size() < zs.total_out) {
50 out->append(buffer, zs.total_out - out->size());
51 }
52 } while (status == Z_OK);
53
54 inflateEnd(&zs);
55
56 if (status != Z_STREAM_END) { // an error occurred that was not EOF
57 VLOG(1) << "Exception during zlib decompression: (" << status << ") "
58 << zs.msg;
59 return false;
60 }
61
62 return true;
63}
64
65inline bool GzipString(absl::string_view uncompressed,
66 std::string* compressed) {
67 z_stream zs;
68 zs.zalloc = Z_NULL;
69 zs.zfree = Z_NULL;
70 zs.opaque = Z_NULL;
71 zs.next_in = Z_NULL;
72 zs.avail_in = 0;
73 zs.next_out = Z_NULL;
74
75 if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) {
76 VLOG(1) << "Cannot initialize zlib compression.";
77 return false;
78 }
79
80 zs.next_in = (Bytef*)uncompressed.data();
81 zs.avail_in = uncompressed.size(); // set the z_stream's input
82
83 int status;
84 char buffer[32768];
85
86 // compress block by block.
87 do {
88 zs.next_out = reinterpret_cast<Bytef*>(buffer);
89 zs.avail_out = sizeof(buffer);
90
91 status = deflate(&zs, Z_FINISH);
92
93 if (compressed->size() < zs.total_out) {
94 compressed->append(buffer, zs.total_out - compressed->size());
95 }
96 } while (status == Z_OK);
97
98 deflateEnd(&zs);
99
100 if (status != Z_STREAM_END) { // an error occurred that was not EOF
101 VLOG(1) << "Exception during zlib compression: (" << status << ") "
102 << zs.msg;
103 return false;
104 }
105 return true;
106}
107
108#endif // OR_TOOLS_BASE_GZIPSTRING_H_
bool GunzipString(absl::string_view str, std::string *out)
Definition gzipstring.h:24
bool GzipString(absl::string_view uncompressed, std::string *compressed)
Definition gzipstring.h:65