-
Notifications
You must be signed in to change notification settings - Fork 1
/
ZlibWrapper.hpp
107 lines (84 loc) · 2.63 KB
/
ZlibWrapper.hpp
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
#include <iostream>
#include <fstream>
#include <filesystem>
#ifdef _MSC_VER
#ifdef WIN32
#define ZLIB_WINAPI
#endif
#endif
#include <zlib.h>
#ifndef ZLIBWRAPPER_HDR
#define ZLIBWRAPPER_HDR
namespace ZLibWrapper
{
bool extractGzFile(std::filesystem::path gzFilePath, std::filesystem::path outputPath)
{
// Open the input file (gzipped file)
#ifdef _MSC_VER
gzFile gzFile = gzopen_w(gzFilePath.wstring().c_str(), "rb");
#else
gzFile gzFile = gzopen(gzFilePath.string().c_str(), "rb");
#endif
if (gzFile == nullptr)
{
std::string errmsg = "Can't open the gzip file for reading: " + gzFilePath.string();
throw std::runtime_error(errmsg);
return false;
}
// Open the output file
std::ofstream outputFile(outputPath, std::ios::out | std::ios::binary);
if (!outputFile.is_open())
{
gzclose(gzFile);
std::string errmsg = "Can't open gzip output file: " + outputPath.string();
throw std::runtime_error(errmsg);
return false;
}
const int bufferSize = 1024;
char buffer[bufferSize];
// Read and write data until the end of the gzipped file
int bytesRead;
while ((bytesRead = gzread(gzFile, buffer, bufferSize)) > 0)
{
outputFile.write(buffer, bytesRead);
}
// Check for errors or premature end of file
if (gzeof(gzFile) == 0)
{
gzclose(gzFile);
outputFile.close();
std::string errmsg = "Can't read gzipped file: " + gzFilePath.string();
throw std::runtime_error(errmsg);
return false;
}
// Close the input and output files
gzclose(gzFile);
outputFile.close();
return true;
}
bool packGzFile(const uint8_t* buffer, uintmax_t size, std::filesystem::path gzFilePath)
{
#ifdef _MSC_VER
gzFile file = gzopen_w(gzFilePath.wstring().c_str(), "wb");
#else
gzFile file = gzopen(gzFilePath.string().c_str(), "wb");
#endif
if (file == nullptr)
{
std::string errmsg = "Can't open a gzip file for writing: " + gzFilePath.string();
throw std::runtime_error(errmsg);
return false;
}
int result = gzwrite(file, buffer, size);
if (result <= 0)
{
gzclose(file);
std::string errmsg = "Failed to write data to: " + gzFilePath.string();
throw std::runtime_error(errmsg);
return false;
}
gzclose(file);
return true;
}
}
#endif