-
Notifications
You must be signed in to change notification settings - Fork 3
/
GzipDecompression.cpp
113 lines (90 loc) · 2.48 KB
/
GzipDecompression.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#define ZLIB_CONST
#include <zlib.h>
#include <zconf.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
bool gzip_inflate(const std::string& compressed_bytes, std::string& uncompressed_bytes)
{
if (compressed_bytes.size() == 0)
{
uncompressed_bytes = compressed_bytes;
return true;
}
uncompressed_bytes.clear();
auto full_length = compressed_bytes.size();
auto uncompressed = std::vector<char>(full_length, 0);
auto strm = z_stream{};
strm.next_in = reinterpret_cast<z_const Bytef*>(compressed_bytes.data());
strm.avail_in = compressed_bytes.size();
strm.total_out = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
if (inflateInit2(&strm, (16 + MAX_WBITS)) != Z_OK)
{
return false;
}
auto result = Z_OK;
while (result == Z_OK)
{
// If the output buffer is too small
if (strm.total_out >= uncompressed.size())
{
uncompressed.resize((uncompressed.size() * 3) / 2 + 1, 0);
}
strm.next_out = reinterpret_cast<Bytef*>(uncompressed.data() + strm.total_out);
strm.avail_out = uncompressed.size() - strm.total_out;
// Inflate another chunk.
result = inflate(&strm, Z_SYNC_FLUSH);
}
if (inflateEnd(&strm) != Z_OK)
{
return false;
}
for (auto i = std::size_t{ 0u }; i < strm.total_out; ++i)
{
uncompressed_bytes += uncompressed[i];
}
return true;
}
// Reads a file into memory
bool load_binary_file(const std::string& filename, std::string& contents)
{
// Open the gzip file in binary mode
auto file = std::fstream{ filename, std::ios::binary | std::ios::in };
if (!file.is_open())
{
return false;
}
contents.clear();
// Read all the bytes in the file
char buffer[1024] = {0};
while (file.read(buffer, 1024u))
{
contents.append(buffer, file.gcount());
}
return true;
}
int main()
{
// Read the gzip file data into memory
auto file_data = std::string{};
if (!load_binary_file("test.gz", file_data))
{
std::cout << "Error loading input file." << std::endl;
return 1;
}
// Uncompress the file data
auto data = std::string{};
if (!gzip_inflate(file_data, data))
{
std::cout << "Error decompressing file." << std::endl;
return 1;
}
// Print the data
for (auto i = std::size_t{ 0 }; i < data.size(); ++i)
{
std::cout << data[i];
}
}