Google OR-Tools v9.15
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 ORTOOLS_BASE_GZIPSTRING_H_
15#define ORTOOLS_BASE_GZIPSTRING_H_
16
17#include <zconf.h>
18#include <zlib.h>
19
20#include <string>
21
22#include "absl/log/log.h"
23#include "absl/strings/string_view.h"
24
25inline bool GunzipString(absl::string_view str, std::string* out) {
26 z_stream zs;
27 zs.zalloc = Z_NULL;
28 zs.zfree = Z_NULL;
29 zs.opaque = Z_NULL;
30 zs.next_in = Z_NULL;
31 zs.avail_in = 0;
32 zs.next_out = Z_NULL;
33 if (inflateInit2(&zs, /*window_bits=*/15 + 32) != Z_OK) {
34 return false;
35 }
36
37 zs.next_in = (Bytef*)str.data();
38 zs.avail_in = str.size();
39
40 int status;
41 char buffer[32768];
42
43 // Decompress string by block.
44 do {
45 zs.next_out = reinterpret_cast<Bytef*>(buffer);
46 zs.avail_out = sizeof(buffer);
47
48 status = inflate(&zs, 0);
49
50 if (out->size() < zs.total_out) {
51 out->append(buffer, zs.total_out - out->size());
52 }
53 } while (status == Z_OK);
54
55 inflateEnd(&zs);
56
57 if (status != Z_STREAM_END) { // an error occurred that was not EOF
58 VLOG(1) << "Exception during zlib decompression: (" << status << ") "
59 << zs.msg;
60 return false;
61 }
62
63 return true;
64}
65
66inline bool GzipString(absl::string_view uncompressed,
67 std::string* compressed) {
68 z_stream zs;
69 zs.zalloc = Z_NULL;
70 zs.zfree = Z_NULL;
71 zs.opaque = Z_NULL;
72 zs.next_in = Z_NULL;
73 zs.avail_in = 0;
74 zs.next_out = Z_NULL;
75
76 if (deflateInit(&zs, Z_BEST_COMPRESSION) != Z_OK) {
77 VLOG(1) << "Cannot initialize zlib compression.";
78 return false;
79 }
80
81 zs.next_in = (Bytef*)uncompressed.data();
82 zs.avail_in = uncompressed.size(); // set the z_stream's input
83
84 int status;
85 char buffer[32768];
86
87 // compress block by block.
88 do {
89 zs.next_out = reinterpret_cast<Bytef*>(buffer);
90 zs.avail_out = sizeof(buffer);
91
92 status = deflate(&zs, Z_FINISH);
93
94 if (compressed->size() < zs.total_out) {
95 compressed->append(buffer, zs.total_out - compressed->size());
96 }
97 } while (status == Z_OK);
98
99 deflateEnd(&zs);
100
101 if (status != Z_STREAM_END) { // an error occurred that was not EOF
102 VLOG(1) << "Exception during zlib compression: (" << status << ") "
103 << zs.msg;
104 return false;
105 }
106 return true;
107}
108
109#endif // ORTOOLS_BASE_GZIPSTRING_H_
bool GunzipString(absl::string_view str, std::string *out)
Definition gzipstring.h:25
bool GzipString(absl::string_view uncompressed, std::string *compressed)
Definition gzipstring.h:66