-
Notifications
You must be signed in to change notification settings - Fork 9
/
extractionworker.cpp
105 lines (88 loc) · 3.14 KB
/
extractionworker.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
/*
* Copyright (c) 2015-2016 Joshua Snyder
* Distributed under the GNU GPL v3. For full terms see the file LICENSE
*
* This file is part of Shticker Book Rewritten.
*
* Shticker Book Rewritten is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Shticker Book Rewritten is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Shticker Book Rewritten. If not, see <http://www.gnu.org/licenses/>.
*/
#include "extractionworker.h"
#include <bzlib.h>
#include <QFile>
#include <QDebug>
ExtractionWorker::ExtractionWorker(QObject *parent) : QObject(parent)
{
}
bool ExtractionWorker::extractBz2(QString compressedFileName, QString extractedFileName)
{
qDebug() << "Extracting" << compressedFileName << "to" << extractedFileName;
FILE *compressedFile;
BZFILE *bzFile;
int bzError;
int bufferBytesRead;
char buffer[1024];
QByteArray decompressedData;
QFile extractedFile(extractedFileName);
//open the destination file
if(!extractedFile.open(QFile::WriteOnly))
{
qDebug() << "Unable to open" << extractedFile.fileName();
return false;
}
//open the compressed file
compressedFile = fopen(compressedFileName.toStdString().c_str(), "rb");
if(!compressedFile)
{
qDebug() << "Error opening file: " << compressedFileName << endl;
return false;
}
bzFile = BZ2_bzReadOpen(&bzError, compressedFile, 0, 0, NULL, 0);
if(bzError != BZ_OK)
{
qDebug() << "Error opening file to extract:" << compressedFileName;
return false;
}
//decompress the file in segments to save RAM
while(bzError == BZ_OK)
{
//decompress the file in increments of 1024 bytes
bufferBytesRead = BZ2_bzRead(&bzError, bzFile, buffer, 1024);
if(bzError == BZ_OK || bzError == BZ_STREAM_END)
{
for(int i=0; i<bufferBytesRead; i++)
decompressedData.append(buffer[i]); //read the buffer into a QByteArray
//write the data to the local file
extractedFile.write(decompressedData);
//clear the QByteArray for the next set of buffer data
decompressedData.clear();
}
}
//purge the extracted file now that we are done with it
extractedFile.flush();
extractedFile.close();
extractedFile.deleteLater();
//check if there were any errors while decompressing
if(bzError != BZ_STREAM_END)
{
BZ2_bzReadClose(&bzError, bzFile);
qDebug() << "Error reading compressed file" << compressedFileName << ". BZ error code:" << bzError << endl;
return false;
}
else
{
BZ2_bzReadClose(&bzError, bzFile);
}
qDebug() << "Finished extracting" << extractedFileName;
return true;
}