1 /*
2 SPDX-FileCopyrightText: 2011-2014 Graeme Gott <graeme@gottcode.org>
3
4 SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include "gzip.h"
8
9 #include <QByteArray>
10 #include <QFile>
11
12 #include <algorithm>
13
14 #include <zlib.h>
15
16 //-----------------------------------------------------------------------------
17
gzip(const QString & path)18 void gzip(const QString& path)
19 {
20 QFile file(path);
21 if (!file.open(QFile::ReadOnly)) {
22 return;
23 }
24 QByteArray data = file.readAll();
25 file.close();
26
27 if (!file.open(QFile::WriteOnly)) {
28 return;
29 }
30 gzFile gz = gzdopen(file.handle(), "wb9");
31 if (!gz) {
32 return;
33 }
34
35 gzwrite(gz, data.constData(), data.size());
36 gzclose(gz);
37 }
38
39 //-----------------------------------------------------------------------------
40
gunzip(const QString & path)41 QByteArray gunzip(const QString& path)
42 {
43 QByteArray data;
44
45 QFile file(path);
46 if (!file.open(QFile::ReadOnly)) {
47 return data;
48 }
49 gzFile gz = gzdopen(file.handle(), "rb");
50 if (!gz) {
51 return data;
52 }
53
54 static const int buffer_size = 0x40000;
55 char buffer[buffer_size];
56 memset(buffer, 0, buffer_size);
57 int read = 0;
58 do {
59 data.append(buffer, read);
60 read = std::min(gzread(gz, buffer, buffer_size), buffer_size);
61 } while (read > 0);
62 gzclose(gz);
63
64 return data;
65 }
66
67 //-----------------------------------------------------------------------------
68