Added zpos attribute to all elements. Changed architecture to allow detailscreen...
[jspeed] / src / zipreader.cpp
1 /*
2  * This file is part of jSpeed.
3  *
4  * jSpeed is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * jSpeed is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with jSpeed.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtCore/QString>
20 #include <QtCore/QByteArray>
21 #include <QtCore/QDebug>
22 #include "zipreader.h"
23
24 namespace
25 {
26     int const BUFFER_SIZE = 16384;
27 }
28
29 ZipReader::ZipReader(QString const& filename): Reader(),
30 filename_(filename), zip_(0), error_("")
31 {
32 }
33
34 bool ZipReader::open()
35 {
36     int error = 0;
37
38     zip_ = zip_open(filename_.toLatin1().data(), 0, &error);
39
40     if(error)
41     {
42         error_ = "Not a valid zip file";
43         zip_ = 0;
44         return false;
45     }
46
47     return true;
48 }
49
50 bool ZipReader::readFile(QString const& filename, QByteArray& data)
51 {
52     zip_file* file = 0;
53
54     file = zip_fopen(zip_, filename.toLatin1().data(), 0);
55
56     if(!file)
57     {
58         error_ = QString(zip_strerror(zip_));
59         return false;
60     }
61
62     char buffer[BUFFER_SIZE];
63
64     int read = 0;
65
66     data.clear();
67
68     while((read = zip_fread(file, buffer, BUFFER_SIZE)) > 0)
69     {
70         data.append(buffer, read);
71     }
72
73     return true;
74 }
75
76 bool ZipReader::fileExists(QString const& filename) const
77 {
78     return zip_name_locate(zip_, filename.toLatin1().data(), 0) != -1;
79 }
80
81 bool ZipReader::close()
82 {
83     if(zip_close(zip_) == 0)
84     {
85         zip_ = 0;
86         return true;
87     }
88
89     return false;
90 }
91
92 QString const& ZipReader::errorString() const
93 {
94     return error_;
95 }